Micron Document
<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Control flow</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Control_flow"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.math.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Control_flow rootpage-Control_flow skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Control flow</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<style data-mw-deduplicate="TemplateStyles:r1236090951">
/* start https://en.wikipedia.org/ */


.mw-parser-output .hatnote{font-style:italic}.mw-parser-output div.hatnote{padding-left:1.6em;margin-bottom:0.5em}.mw-parser-output .hatnote i{font-style:normal}.mw-parser-output .hatnote+link+.hatnote{margin-top:-0.5em}@media print{body.ns-0 .mw-parser-output .hatnote{display:none!important}}


/* end https://en.wikipedia.org/ */
</style><div role="note" class="hatnote navigation-not-searchable">Not to be confused with <a href="Flow_control_(data)" title="Flow control (data)">Flow control (data)</a>.</div>
<style data-mw-deduplicate="TemplateStyles:r1129693374">
/* start https://en.wikipedia.org/ */


.mw-parser-output .hlist dl,.mw-parser-output .hlist ol,.mw-parser-output .hlist ul{margin:0;padding:0}.mw-parser-output .hlist dd,.mw-parser-output .hlist dt,.mw-parser-output .hlist li{margin:0;display:inline}.mw-parser-output .hlist.inline,.mw-parser-output .hlist.inline dl,.mw-parser-output .hlist.inline ol,.mw-parser-output .hlist.inline ul,.mw-parser-output .hlist dl dl,.mw-parser-output .hlist dl ol,.mw-parser-output .hlist dl ul,.mw-parser-output .hlist ol dl,.mw-parser-output .hlist ol ol,.mw-parser-output .hlist ol ul,.mw-parser-output .hlist ul dl,.mw-parser-output .hlist ul ol,.mw-parser-output .hlist ul ul{display:inline}.mw-parser-output .hlist .mw-empty-li{display:none}.mw-parser-output .hlist dt::after{content:": "}.mw-parser-output .hlist dd::after,.mw-parser-output .hlist li::after{content:" · ";font-weight:bold}.mw-parser-output .hlist dd:last-child::after,.mw-parser-output .hlist dt:last-child::after,.mw-parser-output .hlist li:last-child::after{content:none}.mw-parser-output .hlist dd dd:first-child::before,.mw-parser-output .hlist dd dt:first-child::before,.mw-parser-output .hlist dd li:first-child::before,.mw-parser-output .hlist dt dd:first-child::before,.mw-parser-output .hlist dt dt:first-child::before,.mw-parser-output .hlist dt li:first-child::before,.mw-parser-output .hlist li dd:first-child::before,.mw-parser-output .hlist li dt:first-child::before,.mw-parser-output .hlist li li:first-child::before{content:" (";font-weight:normal}.mw-parser-output .hlist dd dd:last-child::after,.mw-parser-output .hlist dd dt:last-child::after,.mw-parser-output .hlist dd li:last-child::after,.mw-parser-output .hlist dt dd:last-child::after,.mw-parser-output .hlist dt dt:last-child::after,.mw-parser-output .hlist dt li:last-child::after,.mw-parser-output .hlist li dd:last-child::after,.mw-parser-output .hlist li dt:last-child::after,.mw-parser-output .hlist li li:last-child::after{content:")";font-weight:normal}.mw-parser-output .hlist ol{counter-reset:listitem}.mw-parser-output .hlist ol>li{counter-increment:listitem}.mw-parser-output .hlist ol>li::before{content:" "counter(listitem)"\a0 "}.mw-parser-output .hlist dd ol>li:first-child::before,.mw-parser-output .hlist dt ol>li:first-child::before,.mw-parser-output .hlist li ol>li:first-child::before{content:" ("counter(listitem)"\a0 "}


/* end https://en.wikipedia.org/ */
</style><style data-mw-deduplicate="TemplateStyles:r1246091330">
/* start https://en.wikipedia.org/ */


.mw-parser-output .sidebar{width:22em;float:right;clear:right;margin:0.5em 0 1em 1em;background:var(--background-color-neutral-subtle,#f8f9fa);border:1px solid var(--border-color-base,#a2a9b1);padding:0.2em;text-align:center;line-height:1.4em;font-size:88%;border-collapse:collapse;display:table}body.skin-minerva .mw-parser-output .sidebar{display:table!important;float:right!important;margin:0.5em 0 1em 1em!important}.mw-parser-output .sidebar-subgroup{width:100%;margin:0;border-spacing:0}.mw-parser-output .sidebar-left{float:left;clear:left;margin:0.5em 1em 1em 0}.mw-parser-output .sidebar-none{float:none;clear:both;margin:0.5em 1em 1em 0}.mw-parser-output .sidebar-outer-title{padding:0 0.4em 0.2em;font-size:125%;line-height:1.2em;font-weight:bold}.mw-parser-output .sidebar-top-image{padding:0.4em}.mw-parser-output .sidebar-top-caption,.mw-parser-output .sidebar-pretitle-with-top-image,.mw-parser-output .sidebar-caption{padding:0.2em 0.4em 0;line-height:1.2em}.mw-parser-output .sidebar-pretitle{padding:0.4em 0.4em 0;line-height:1.2em}.mw-parser-output .sidebar-title,.mw-parser-output .sidebar-title-with-pretitle{padding:0.2em 0.8em;font-size:145%;line-height:1.2em}.mw-parser-output .sidebar-title-with-pretitle{padding:0.1em 0.4em}.mw-parser-output .sidebar-image{padding:0.2em 0.4em 0.4em}.mw-parser-output .sidebar-heading{padding:0.1em 0.4em}.mw-parser-output .sidebar-content{padding:0 0.5em 0.4em}.mw-parser-output .sidebar-content-with-subgroup{padding:0.1em 0.4em 0.2em}.mw-parser-output .sidebar-above,.mw-parser-output .sidebar-below{padding:0.3em 0.8em;font-weight:bold}.mw-parser-output .sidebar-collapse .sidebar-above,.mw-parser-output .sidebar-collapse .sidebar-below{border-top:1px solid #aaa;border-bottom:1px solid #aaa}.mw-parser-output .sidebar-navbar{text-align:right;font-size:115%;padding:0 0.4em 0.4em}.mw-parser-output .sidebar-list-title{padding:0 0.4em;text-align:left;font-weight:bold;line-height:1.6em;font-size:105%}.mw-parser-output .sidebar-list-title-c{padding:0 0.4em;text-align:center;margin:0 3.3em}@media(max-width:640px){body.mediawiki .mw-parser-output .sidebar{width:100%!important;clear:both;float:none!important;margin-left:0!important;margin-right:0!important}}body.skin--responsive .mw-parser-output .sidebar a>img{max-width:none!important}@media screen{html.skin-theme-clientpref-night .mw-parser-output .sidebar:not(.notheme) .sidebar-list-title,html.skin-theme-clientpref-night .mw-parser-output .sidebar:not(.notheme) .sidebar-title-with-pretitle{background:transparent!important}html.skin-theme-clientpref-night .mw-parser-output .sidebar:not(.notheme) .sidebar-title-with-pretitle a{color:var(--color-progressive)!important}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .sidebar:not(.notheme) .sidebar-list-title,html.skin-theme-clientpref-os .mw-parser-output .sidebar:not(.notheme) .sidebar-title-with-pretitle{background:transparent!important}html.skin-theme-clientpref-os .mw-parser-output .sidebar:not(.notheme) .sidebar-title-with-pretitle a{color:var(--color-progressive)!important}}@media print{body.ns-0 .mw-parser-output .sidebar{display:none!important}}


/* end https://en.wikipedia.org/ */
</style><table class="sidebar nomobile nowraplinks"><tbody><tr><th class="sidebar-title"><a class="mw-selflink-fragment" href="#Loops">Loop constructs</a></th></tr><tr><td class="sidebar-content">
<div class="hlist">
<ul><li><a href="Do_while_loop" title="Do while loop">Do while loop</a></li>
<li><a href="While_loop" title="While loop">While loop</a></li>
<li><a href="For_loop" title="For loop">For loop</a></li>
<li><a href="Foreach_loop" title="Foreach loop">Foreach loop</a></li>
<li><a href="Infinite_loop" title="Infinite loop">Infinite loop</a></li>
</ul>
</div></td>
</tr><tr><td class="sidebar-navbar"><style data-mw-deduplicate="TemplateStyles:r1239400231">
/* start https://en.wikipedia.org/ */


.mw-parser-output .navbar{display:inline;font-size:88%;font-weight:normal}.mw-parser-output .navbar-collapse{float:left;text-align:left}.mw-parser-output .navbar-boxtext{word-spacing:0}.mw-parser-output .navbar ul{display:inline-block;white-space:nowrap;line-height:inherit}.mw-parser-output .navbar-brackets::before{margin-right:-0.125em;content:"[ "}.mw-parser-output .navbar-brackets::after{margin-left:-0.125em;content:" ]"}.mw-parser-output .navbar li{word-spacing:-0.125em}.mw-parser-output .navbar a>span,.mw-parser-output .navbar a>abbr{text-decoration:inherit}.mw-parser-output .navbar-mini abbr{font-variant:small-caps;border-bottom:none;text-decoration:none;cursor:inherit}.mw-parser-output .navbar-ct-full{font-size:114%;margin:0 7em}.mw-parser-output .navbar-ct-mini{font-size:114%;margin:0 4em}html.skin-theme-clientpref-night .mw-parser-output .navbar li a abbr{color:var(--color-base)!important}@media(prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .navbar li a abbr{color:var(--color-base)!important}}@media print{.mw-parser-output .navbar{display:none!important}}


/* end https://en.wikipedia.org/ */
</style></td></tr></tbody></table>
<p>In <a href="Computer_science" title="Computer science">computer science</a>, <b>control flow</b> (or <b>flow of control</b>) is the order in which individual <a href="Statement_(computer_science)" title="Statement (computer science)">statements</a>, <a href="Instruction_(computer_science)" class="mw-redirect" title="Instruction (computer science)">instructions</a> or <a href="Function_call" class="mw-redirect" title="Function call">function calls</a> of an <a href="Imperative_programming" title="Imperative programming">imperative</a> <a href="Computer_program" title="Computer program">program</a> are <a href="Execution_(computing)" title="Execution (computing)">executed</a> or evaluated. The emphasis on explicit control flow distinguishes an <i><a href="Imperative_programming" title="Imperative programming">imperative programming</a></i> language from a <i><a href="Declarative_programming" title="Declarative programming">declarative programming</a></i> language.
</p><p>Within an imperative <a href="Programming_language" title="Programming language">programming language</a>, a <i>control flow statement</i> is a statement that results in a choice being made as to which of two or more paths to follow. For <a href="Strict_programming_language" title="Strict programming language">non-strict</a> functional languages, functions and <a href="Language_construct" title="Language construct">language constructs</a> exist to achieve the same result, but they are usually not termed control flow statements.
</p><p>A set of statements is in turn generally structured as a <a href="Block_(programming)" title="Block (programming)">block</a>, which in addition to grouping, also defines a <a href="Lexical_scope" class="mw-redirect" title="Lexical scope">lexical scope</a>.
</p><p><a href="Interrupt" title="Interrupt">Interrupts</a> and <a href="Signal_(computing)" class="mw-redirect" title="Signal (computing)">signals</a> are low-level mechanisms that can alter the flow of control in a way similar to a <a href="Subroutine" class="mw-redirect" title="Subroutine">subroutine</a>, but usually occur as a response to some external stimulus or event (that can occur <a href="Asynchronous_systems" class="mw-redirect" title="Asynchronous systems">asynchronously</a>), rather than execution of an <i>in-line</i> control flow statement.
</p><p>At the level of <a href="Machine_language" class="mw-redirect" title="Machine language">machine language</a> or <a href="Assembly_language" title="Assembly language">assembly language</a>, control flow instructions usually work by altering the <a href="Program_counter" title="Program counter">program counter</a>. For some <a href="Central_processing_unit" title="Central processing unit">central processing units</a> (CPUs), the only control flow instructions available are conditional or unconditional <a href="Branch_(computer_science)" title="Branch (computer science)">branch</a> instructions, also termed jumps. However there is also <a href="Predication_(computer_architecture)" title="Predication (computer architecture)">predication</a> which conditionally enables or disables instructions <i>without</i> branching: as an alternative technique it can have both <a href="Predication_(computer_architecture)#Advantages" title="Predication (computer architecture)">advantages</a> and disadvantages over branching.
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Categories">Categories</h2></div>

<p>The kinds of control flow statements supported by different languages vary, but can be categorized by their effect:
</p>
<ul><li>Continuation at a different statement (unconditional <a href="Branch_(computer_science)" title="Branch (computer science)">branch</a> or jump)</li>
<li>Executing a set of statements only if some condition is met (choice - i.e., <a href="Conditional_branch" class="mw-redirect" title="Conditional branch">conditional branch</a>)</li>
<li>Executing a set of statements zero or more times, until some condition is met (i.e., loop - the same as <a href="Conditional_branch" class="mw-redirect" title="Conditional branch">conditional branch</a>)</li>
<li>Executing a set of distant statements, after which the flow of control usually returns (<a href="Subroutine" class="mw-redirect" title="Subroutine">subroutines</a>, <a href="Coroutine" title="Coroutine">coroutines</a>, and <a href="Continuation" title="Continuation">continuations</a>)</li>
<li>Stopping the program, preventing any further execution (unconditional halt)</li></ul>
<div class="mw-heading mw-heading2"><h2 id="Primitives">Primitives</h2></div>
<div class="mw-heading mw-heading3"><h3 id="Labels">Labels</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Label_(computer_science)" title="Label (computer science)">Label (computer science)</a></div>
<p>A <a href="Label_(programming_language)" class="mw-redirect" title="Label (programming language)">label</a> is an explicit name or number assigned to a fixed position within the <a href="Source_code" title="Source code">source code</a>, and which may be referenced by control flow statements appearing elsewhere in the source code. A label marks a position within source code and has no other effect.
</p><p><a href="Line_number" title="Line number">Line numbers</a> are an alternative to a named label used in some languages (such as <a href="BASIC" title="BASIC">BASIC</a>). They are <a href="Natural_number" title="Natural number">whole numbers</a> placed at the start of each line of text in the source code. Languages which use these often impose the constraint that the line numbers must increase in value in each following line, but may not require that they be consecutive. For example, in BASIC:
</p>
<div class="mw-highlight mw-highlight-lang-qbasic mw-content-ltr" dir="ltr"><pre><span class="nl">10</span><span class="w"> </span><span class="kd">LET</span><span class="w"> </span><span class="vg">X</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="il">3</span>
<span class="nl">20</span><span class="w"> </span><span class="kr">PRINT</span><span class="w"> </span><span class="vg">X</span>
</pre></div>
<p>In other languages such as <a href="C_(programming_language)" title="C (programming language)">C</a> and <a href="Ada_(programming_language)" title="Ada (programming language)">Ada</a>, a label is an <a href="Identifier" title="Identifier">identifier</a>, usually appearing at the start of a line and immediately followed by a colon. For example, in C:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="nl">Success</span><span class="p">:</span><span class="w"> </span><span class="n">printf</span><span class="p">(</span><span class="s">"The operation was successful.</span><span class="se">\n</span><span class="s">"</span><span class="p">);</span>
</pre></div>
<p>The language <a href="ALGOL_60" title="ALGOL 60">ALGOL 60</a> allowed both whole numbers and identifiers as labels (both linked by colons to the following statement), but few if any other <a href="ALGOL" title="ALGOL">ALGOL</a> variants allowed whole numbers. Early <a href="Fortran" title="Fortran">Fortran</a> compilers only allowed whole numbers as labels. Beginning with Fortran-90, alphanumeric labels have also been allowed.
</p>
<div class="mw-heading mw-heading3"><h3 id="Goto">Goto</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="GOTO" class="mw-redirect" title="GOTO">GOTO</a></div>
<p>The <i>goto</i> statement (a combination of the English words <i><a href="https://en.wiktionary.org/wiki/go" class="extiw external" title="wiktionary:go">go</a></i> and <i><a href="https://en.wiktionary.org/wiki/to" class="extiw external" title="wiktionary:to">to</a></i>, and pronounced accordingly) is the most basic form of unconditional transfer of control.
</p><p>Although the <a href="Keyword_(computing)" class="mw-redirect" title="Keyword (computing)">keyword</a> may either be in upper or lower case depending on the language, it is usually written as:
</p>
<pre> <b>goto</b> <i>label</i>
</pre>
<p>The effect of a goto statement is to cause the next statement to be executed to be the statement appearing at (or immediately after) the indicated label.
</p><p>Goto statements have been <a href="Considered_harmful" title="Considered harmful">considered harmful</a> by many computer scientists, notably <a href="Edsger_W._Dijkstra" title="Edsger W. Dijkstra">Dijkstra</a>.
</p>
<div class="mw-heading mw-heading3"><h3 id="Subroutines">Subroutines</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Subroutine" class="mw-redirect" title="Subroutine">Subroutine</a></div>
<p>The terminology for <a href="Subroutine" class="mw-redirect" title="Subroutine">subroutines</a> varies; they may alternatively be known as routines, procedures, functions (especially if they return results) or methods (especially if they belong to <a href="Class_(programming)" class="mw-redirect" title="Class (programming)">classes</a> or <a href="Type_class" title="Type class">type classes</a>).
</p><p>In the 1950s, computer memories were very small by current standards so subroutines were used mainly to reduce program size. A piece of code was written once and then used many times from various other places in a program.
</p><p>Today, subroutines are more often used to help make a program more structured, e.g., by isolating some algorithm or hiding some data access method. If many programmers are working on one program, subroutines are one kind of <a href="Modularity_(programming)" class="mw-redirect" title="Modularity (programming)">modularity</a> that can help divide the work.
</p>
<div class="mw-heading mw-heading3"><h3 id="Sequence">Sequence</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Structured_programming" title="Structured programming">Structured programming</a></div>
<p>In structured programming, the ordered sequencing of successive commands is considered one of the basic control structures, which is used as a building block for programs alongside iteration, recursion and choice.
</p>
<div class="mw-heading mw-heading2"><h2 id="Minimal_structured_control_flow">Minimal structured control flow</h2></div>
<div role="note" class="hatnote navigation-not-searchable">See also: <a href="Structured_program_theorem" title="Structured program theorem">Structured program theorem</a></div>
<p>In May 1966, Böhm and Jacopini published an article<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> in <i>Communications of the ACM</i> which showed that any program with <b>goto</b>s could be transformed into a goto-free form involving only choice (IF THEN ELSE) and loops (WHILE condition DO xxx), possibly with duplicated code and/or the addition of Boolean variables (true/false flags). Later authors showed that choice can be replaced by loops (and yet more Boolean variables).
</p><p>That such minimalism is possible does not mean that it is necessarily desirable; computers theoretically need only <a href="One_instruction_set_computer" class="mw-redirect" title="One instruction set computer">one machine instruction</a> (subtract one number from another and branch if the result is negative), but practical computers have dozens or even hundreds of machine instructions.
</p><p>Other research showed that control structures with one entry and one exit were much easier to understand than any other form, mainly because they could be used anywhere as a statement without disrupting the control flow. In other words, they were <i>composable</i>. (Later developments, such as <a href="Non-strict_programming_language" class="mw-redirect" title="Non-strict programming language">non-strict programming languages</a> – and more recently, composable <a href="Software_transactional_memory" title="Software transactional memory">software transactions</a> – have continued this strategy, making components of programs even more freely composable.)
</p><p>Some academics took a purist approach to the Böhm–Jacopini result and argued that even instructions like <code>break</code> and <code>return</code> from the middle of loops are bad practice as they are not needed in the Böhm–Jacopini proof, and thus they advocated that all loops should have a single exit point. This purist approach is embodied in the language <a href="Pascal_(programming_language)" title="Pascal (programming language)">Pascal</a> (designed in 1968–1969), which up to the mid-1990s was the preferred tool for teaching introductory programming in academia.<sup id="cite_ref-roberts_2-0" class="reference"><a href="#cite_note-roberts-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> The direct application of the Böhm–Jacopini theorem may result in additional local variables being introduced in the structured chart, and may also result in some <a href="Code_duplication" class="mw-redirect" title="Code duplication">code duplication</a>.<sup id="cite_ref-WattFindlay2004_3-0" class="reference"><a href="#cite_note-WattFindlay2004-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup> Pascal is affected by both of these problems and according to empirical studies cited by <a href="Eric_S._Roberts" title="Eric S. Roberts">Eric S. Roberts</a>, student programmers had difficulty formulating correct solutions in Pascal for several simple problems, including writing a function for searching an element in an array. A 1980 study by Henry Shapiro cited by Roberts found that using only the Pascal-provided control structures, the correct solution was given by only 20% of the subjects, while no subject wrote incorrect code for this problem if allowed to write a return from the middle of a loop.<sup id="cite_ref-roberts_2-1" class="reference"><a href="#cite_note-roberts-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="Control_structures_in_practice">Control structures in practice</h2></div>
<p>Most programming languages with control structures have an initial keyword which indicates the type of control structure involved. Languages then divide as to whether or not control structures have a final keyword.
</p>
<ul><li>No final keyword: <a href="ALGOL_60" title="ALGOL 60">ALGOL 60</a>, <a href="C_(programming_language)" title="C (programming language)">C</a>, <a href="C%2B%2B" title="C++">C++</a>, <a href="Go_(programming_language)" title="Go (programming language)">Go</a>, <a href="Haskell" title="Haskell">Haskell</a>, <a href="Java_(programming_language)" title="Java (programming language)">Java</a>, <a href="Pascal_(programming_language)" title="Pascal (programming language)">Pascal</a>, <a href="Perl" title="Perl">Perl</a>, <a href="PHP" title="PHP">PHP</a>, <a href="PL/I" title="PL/I">PL/I</a>, <a href="Python_(programming_language)" title="Python (programming language)">Python</a>, <a href="PowerShell" title="PowerShell">PowerShell</a>. Such languages need some way of grouping statements together:
<ul><li>ALGOL 60 and Pascal: <code>begin</code> ... <code>end</code></li>
<li>C, C++, Go, Java, Perl, PHP, and PowerShell: <a href="Curly_bracket_programming_language" class="mw-redirect" title="Curly bracket programming language">curly brackets</a> <code>{</code> ... <code>}</code></li>
<li>PL/I: <code>DO</code> ... <code>END</code></li>
<li>Python: uses <a href="Indent_style" class="mw-redirect" title="Indent style">indent</a> level (see <a href="Off-side_rule" title="Off-side rule">Off-side rule</a>)</li>
<li>Haskell: either <a href="Indent_style" class="mw-redirect" title="Indent style">indent</a> level or curly brackets can be used, and they can be freely mixed</li>
<li>Lua: uses <code>do</code> ... <code>end</code></li></ul></li>
<li>Final keyword: <a href="Ada_(programming_language)" title="Ada (programming language)">Ada</a>, <a href="APL_(programming_language)" title="APL (programming language)">APL</a>, <a href="ALGOL_68" title="ALGOL 68">ALGOL 68</a>, <a href="Modula-2" title="Modula-2">Modula-2</a>, <a href="Fortran_77" class="mw-redirect" title="Fortran 77">Fortran 77</a>, Mythryl, <a href="Visual_Basic" title="Visual Basic">Visual Basic</a>. The forms of the final keyword vary:
<ul><li>Ada: final keyword is <code>end</code> + <i>space</i> + initial keyword e.g., <code>if</code> ... <code>end if</code>, <code>loop</code> ... <code>end loop</code></li>
<li>APL: final keyword is <code>:End</code> optionally + initial keyword, e.g., <code>:If</code> ... <code>:End</code> or <code>:If</code> ... <code>:EndIf</code>, <code>Select</code> ... <code>:End</code> or <code>:Select</code> ... <code>:EndSelect</code>, however, if adding an end condition, the end keyword becomes <code>:Until</code></li>
<li>ALGOL 68, Mythryl: initial keyword spelled backwards e.g., <code>if</code> ... <code>fi</code>, <code>case</code> ... <code>esac</code></li>
<li>Fortran 77: final keyword is <code>END</code> + initial keyword e.g., <code>IF</code> ... <code>ENDIF</code>, <code>DO</code> ... <code>ENDDO</code></li>
<li>Modula-2: same final keyword <code>END</code> for everything</li>
<li>Visual Basic: every control structure has its own keyword. <code>If</code> ... <code>End If</code>; <code>For</code> ... <code>Next</code>; <code>Do</code> ... <code>Loop</code>; <code>While</code> ... <code>Wend</code></li></ul></li></ul>
<div class="mw-heading mw-heading2"><h2 id="Choice">Choice</h2></div>
<div class="mw-heading mw-heading3"><h3 id="If-then-(else)_statements">If-then-(else) statements</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Conditional_(computer_programming)" title="Conditional (computer programming)">Conditional (computer programming)</a></div>
<p>Conditional expressions and conditional constructs are features of a <a href="Programming_language" title="Programming language">programming language</a> that perform different computations or actions depending on whether a programmer-specified <a href="Boolean_data_type" title="Boolean data type">Boolean</a> <i>condition</i> evaluates to true or false.
</p>
<ul><li><code>IF..GOTO</code>. A form found in unstructured languages, mimicking a typical machine code instruction, would jump to (GOTO) a label or line number when the condition was met.</li>
<li><code>IF..THEN..(ENDIF)</code>. Rather than being restricted to a jump, any simple statement, or nested block, could follow the THEN key keyword. This a structured form.</li>
<li><code>IF..THEN..ELSE..(ENDIF)</code>. As above, but with a second action to be performed if the condition is false. This is one of the most common forms, with many variations. Some require a terminal <code>ENDIF</code>, others do not. <a href="C_(programming_language)" title="C (programming language)">C</a> and related languages do not require a terminal keyword, or a 'then', but do require parentheses around the condition.</li>
<li>Conditional statements can be and often are nested inside other conditional statements. Some languages allow <code>ELSE</code> and <code>IF</code> to be combined into <code>ELSEIF</code>, avoiding the need to have a series of <code>ENDIF</code> or other final statements at the end of a compound statement.</li></ul>
<table class="wikitable">

<tbody><tr>
<th><a href="Pascal_(programming_language)" title="Pascal (programming language)">Pascal</a>:
</th>
<th><a href="Ada_(programming_language)" title="Ada (programming language)">Ada</a>:
</th></tr>
<tr>
<td><div class="mw-highlight mw-highlight-lang-pascal mw-content-ltr" dir="ltr"><pre><span class="k">if</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">0</span><span class="w"> </span><span class="k">then</span>
<span class="w"> </span><span class="nb">writeln</span><span class="p">(</span><span class="err">"</span><span class="n">yes</span><span class="err">"</span><span class="p">)</span>
<span class="k">else</span>
<span class="w"> </span><span class="nb">writeln</span><span class="p">(</span><span class="err">"</span><span class="n">no</span><span class="err">"</span><span class="p">)</span><span class="o">;</span>
</pre></div>
</td>
<td><div class="mw-highlight mw-highlight-lang-ada mw-content-ltr" dir="ltr"><pre><span class="kr">if</span> <span class="n">a</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="kr">then</span>
<span class="n">Put_Line</span><span class="p">(</span><span class="s">"yes"</span><span class="p">);</span>
<span class="kr">else</span>
<span class="n">Put_Line</span><span class="p">(</span><span class="s">"no"</span><span class="p">);</span>
<span class="kr">end</span> <span class="kr">if</span><span class="p">;</span>
</pre></div>
</td></tr>
<tr>
<th><a href="C_(programming_language)" title="C (programming language)">C</a>:
</th>
<th><a href="Shell_script" title="Shell script">Shell script</a>:
</th></tr>
<tr>
<td><div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">a</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span>
<span class="w"> </span><span class="n">puts</span><span class="p">(</span><span class="s">"yes"</span><span class="p">);</span>
<span class="p">}</span>
<span class="k">else</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">puts</span><span class="p">(</span><span class="s">"no"</span><span class="p">);</span>
<span class="p">}</span>
</pre></div>
</td>
<td><div class="mw-highlight mw-highlight-lang-bash mw-content-ltr" dir="ltr"><pre><span class="k">if</span><span class="w"> </span><span class="o">[</span><span class="w"> </span><span class="nv">$a</span><span class="w"> </span>-gt<span class="w"> </span><span class="m">0</span><span class="w"> </span><span class="o">]</span><span class="p">;</span><span class="w"> </span><span class="k">then</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"yes"</span>
<span class="k">else</span>
<span class="w"> </span><span class="nb">echo</span><span class="w"> </span><span class="s2">"no"</span>
<span class="k">fi</span>
</pre></div>
</td></tr>
<tr>
<th><a href="Python_(programming_language)" title="Python (programming language)">Python</a>:
</th>
<th><a href="Lisp_(programming_language)" title="Lisp (programming language)">Lisp</a>:
</th></tr>
<tr>
<td><div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span class="k">if</span> <span class="n">a</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"yes"</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"no"</span><span class="p">)</span>
</pre></div>
</td>
<td><div class="mw-highlight mw-highlight-lang-lisp mw-content-ltr" dir="ltr"><pre><span class="p">(</span><span class="nb">princ</span>
<span class="w"> </span><span class="p">(</span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="nb">plusp</span><span class="w"> </span><span class="nv">a</span><span class="p">)</span>
<span class="w"> </span><span class="s">"yes"</span>
<span class="w"> </span><span class="s">"no"</span><span class="p">))</span>
</pre></div>
</td></tr></tbody></table>
<p>Less common variations include:
</p>
<ul><li>Some languages, such as early <a href="Fortran" title="Fortran">Fortran</a>,<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>a<span class="cite-bracket">]</span></a></sup> have a <i>three-way</i> or <i><a href="Arithmetic_if" class="mw-redirect" title="Arithmetic if">arithmetic if</a></i>, testing whether a numeric value is negative, zero, or positive.</li>
<li>Some languages have a <a href="Functional_programming" title="Functional programming">functional</a> form of an <code>if</code> statement, for instance <a href="Lisp_(programming_language)" title="Lisp (programming language)">Lisp's</a> <code>cond</code>.</li>
<li>Some languages have an <a href="Operator_(programming)" class="mw-redirect" title="Operator (programming)">operator</a> form of an <code>if</code> statement, such as C's <a href="Ternary_operator" class="mw-redirect" title="Ternary operator">ternary operator</a>.</li>
<li><a href="Perl" title="Perl">Perl</a> supplements a C-style <code>if</code> with <code>when</code> and <code>unless</code>.</li>
<li><a href="Smalltalk" title="Smalltalk">Smalltalk</a> uses <code>ifTrue</code> and <code>ifFalse</code> messages to implement conditionals, rather than any fundamental language construct.</li></ul>
<div class="mw-heading mw-heading3"><h3 id="Case_and_switch_statements">Case and switch statements</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Switch_statement" title="Switch statement">Switch statement</a></div>
<p><a href="Switch_statement" title="Switch statement">Switch statements</a> (or <i>case statements</i>, or <i>multiway branches</i>) compare a given value with specified constants and take action according to the first constant to match. There is usually a provision for a default action ("else", "otherwise") to be taken if no match succeeds. Switch statements can allow compiler optimizations, such as <a href="Lookup_table" title="Lookup table">lookup tables</a>. In <a href="Dynamic_language" class="mw-redirect" title="Dynamic language">dynamic languages</a>, the cases may not be limited to constant expressions, and might extend to <a href="Pattern_matching" title="Pattern matching">pattern matching</a>, as in the <a href="Shell_script" title="Shell script">shell script</a> example on the right, where the <code>*)</code> implements the default case as a <a href="Glob_(programming)" title="Glob (programming)">glob</a> matching any string. Case logic can also be implemented in functional form, as in <a href="SQL" title="SQL">SQL</a>'s <code>decode</code> statement.
</p>
<table class="wikitable">

<tbody><tr>
<th><a href="Pascal_(programming_language)" title="Pascal (programming language)">Pascal</a>:
</th>
<th><a href="Ada_(programming_language)" title="Ada (programming language)">Ada</a>:
</th></tr>
<tr>
<td><div class="mw-highlight mw-highlight-lang-pascal mw-content-ltr" dir="ltr"><pre><span class="k">case</span><span class="w"> </span><span class="n">someChar</span><span class="w"> </span><span class="k">of</span>
<span class="w"> </span><span class="s">'a'</span><span class="o">:</span><span class="w"> </span><span class="n">actionOnA</span><span class="o">;</span>
<span class="w"> </span><span class="s">'x'</span><span class="o">:</span><span class="w"> </span><span class="n">actionOnX</span><span class="o">;</span>
<span class="w"> </span><span class="s">'y'</span><span class="o">,</span><span class="s">'z'</span><span class="o">:</span><span class="n">actionOnYandZ</span><span class="o">;</span>
<span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="n">actionOnNoMatch</span><span class="o">;</span>
<span class="k">end</span><span class="o">;</span>
</pre></div>
</td>
<td><div class="mw-highlight mw-highlight-lang-ada mw-content-ltr" dir="ltr"><pre><span class="kr">case</span> <span class="n">someChar</span> <span class="kr">is</span>
<span class="kr">when</span> <span class="p">'</span><span class="na">a</span><span class="p">'</span> <span class="p">=&gt;</span> <span class="n">actionOnA</span><span class="p">;</span>
<span class="kr">when</span> <span class="p">'</span><span class="na">x</span><span class="p">'</span> <span class="p">=&gt;</span> <span class="n">actionOnX</span><span class="p">;</span>
<span class="kr">when</span> <span class="p">'</span><span class="na">y</span><span class="p">'</span> <span class="p">|</span> <span class="p">'</span><span class="na">z</span><span class="p">'</span> <span class="p">=&gt;</span> <span class="n">actionOnYandZ</span><span class="p">;</span>
<span class="kr">when</span> <span class="kr">others</span> <span class="p">=&gt;</span> <span class="n">actionOnNoMatch</span><span class="p">;</span>
<span class="kr">end</span><span class="p">;</span>
</pre></div>
</td></tr>
<tr>
<th><a href="C_(programming_language)" title="C (programming language)">C</a>:
</th>
<th><a href="Shell_script" title="Shell script">Shell script</a>:
</th></tr>
<tr>
<td><div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="k">switch</span><span class="w"> </span><span class="p">(</span><span class="n">someChar</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="sc">'a'</span><span class="p">:</span><span class="w"> </span><span class="n">actionOnA</span><span class="p">;</span><span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="sc">'x'</span><span class="p">:</span><span class="w"> </span><span class="n">actionOnX</span><span class="p">;</span><span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="sc">'y'</span><span class="p">:</span>
<span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="sc">'z'</span><span class="p">:</span><span class="w"> </span><span class="n">actionOnYandZ</span><span class="p">;</span><span class="w"> </span><span class="k">break</span><span class="p">;</span>
<span class="w"> </span><span class="k">default</span><span class="o">:</span><span class="w"> </span><span class="n">actionOnNoMatch</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</td>
<td><div class="mw-highlight mw-highlight-lang-bash mw-content-ltr" dir="ltr"><pre><span class="k">case</span><span class="w"> </span><span class="nv">$someChar</span><span class="w"> </span><span class="k">in</span><span class="w"> </span>
<span class="w"> </span>a<span class="o">)</span><span class="w"> </span>actionOnA<span class="w"> </span><span class="p">;;</span>
<span class="w"> </span>x<span class="o">)</span><span class="w"> </span>actionOnX<span class="w"> </span><span class="p">;;</span>
<span class="w"> </span><span class="o">[</span>yz<span class="o">])</span><span class="w"> </span>actionOnYandZ<span class="w"> </span><span class="p">;;</span>
<span class="w"> </span>*<span class="o">)</span><span class="w"> </span>actionOnNoMatch<span class="w"> </span><span class="p">;;</span>
<span class="k">esac</span>
</pre></div>
</td></tr>
<tr>
<th><a href="Lisp_(programming_language)" title="Lisp (programming language)">Lisp</a>:
</th>
<th><a href="Fortran" title="Fortran">Fortran</a>:
</th></tr>
<tr>
<td><div class="mw-highlight mw-highlight-lang-lisp mw-content-ltr" dir="ltr"><pre><span class="p">(</span><span class="nb">case</span><span class="w"> </span><span class="nv">some-char</span>
<span class="w"> </span><span class="p">((</span><span class="sc">#\a</span><span class="p">)</span><span class="w"> </span><span class="nv">action-on-a</span><span class="p">)</span>
<span class="w"> </span><span class="p">((</span><span class="sc">#\x</span><span class="p">)</span><span class="w"> </span><span class="nv">action-on-x</span><span class="p">)</span>
<span class="w"> </span><span class="p">((</span><span class="sc">#\y</span><span class="w"> </span><span class="sc">#\z</span><span class="p">)</span><span class="w"> </span><span class="nv">action-on-y-and-z</span><span class="p">)</span>
<span class="w"> </span><span class="p">(</span><span class="nv">else</span><span class="w"> </span><span class="nv">action-on-no-match</span><span class="p">))</span>
</pre></div>
</td>
<td><div class="mw-highlight mw-highlight-lang-fortran mw-content-ltr" dir="ltr"><pre><span class="k">select case</span><span class="w"> </span><span class="p">(</span><span class="n">someChar</span><span class="p">)</span>
<span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="p">(</span><span class="s1">'a'</span><span class="p">)</span>
<span class="w"> </span><span class="n">actionOnA</span>
<span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="p">(</span><span class="s1">'x'</span><span class="p">)</span>
<span class="w"> </span><span class="n">actionOnX</span>
<span class="w"> </span><span class="k">case</span><span class="w"> </span><span class="p">(</span><span class="s1">'y'</span><span class="p">,</span><span class="s1">'z'</span><span class="p">)</span>
<span class="w"> </span><span class="n">actionOnYandZ</span>
<span class="w"> </span><span class="k">case </span><span class="n">default</span>
<span class="w"> </span><span class="n">actionOnNoMatch</span>
<span class="k">end select</span>
</pre></div>
</td></tr></tbody></table>
<div class="mw-heading mw-heading2"><h2 id="Loops">Loops</h2></div>
<p>A loop is a sequence of statements which is specified once but which may be carried out several times in succession. The code "inside" the loop (the <i>body</i> of the loop, shown below as <i>xxx</i>) is obeyed a specified number of times, or once for each of a collection of items (both cases of <i>definite iteration</i>), or until some condition is met (<i>indefinite iteration</i>), or <a href="Infinite_loop" title="Infinite loop">infinitely</a>. When one of those items is itself also a loop, it is called a "nested loop".<sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-7" class="reference"><a href="#cite_note-7"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup>
</p><p>Some common situations are not well-handled by these basic control structures, and are generally addressed by early exit from the loop, in the form of a <i>break</i> from the loop, a <i>return</i> from the function, or an exception being raised.<sup id="cite_ref-8" class="reference"><a href="#cite_note-8"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-roberts_2-2" class="reference"><a href="#cite_note-roberts-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>
</p><p>In <a href="Functional_programming" title="Functional programming">functional programming</a> languages, such as <a href="Haskell" title="Haskell">Haskell</a> and <a href="Scheme_(programming_language)" title="Scheme (programming language)">Scheme</a>, both <a href="Recursion_(computer_science)" title="Recursion (computer science)">recursive</a> and <a href="Fixed_point_combinator" class="mw-redirect" title="Fixed point combinator">iterative</a> processes are expressed with <a href="Tail_recursion" class="mw-redirect" title="Tail recursion">tail recursive</a> procedures instead of looping constructs that are syntactic.
</p>
<div class="mw-heading mw-heading3"><h3 id="Count-controlled_loops">Count-controlled loops</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="For_loop" title="For loop">For loop</a></div>
<p>Most programming languages have constructions for repeating a loop a certain number of times.
In most cases counting can go downwards instead of upwards and step sizes other than 1 can be used.
</p>
<table class="wikitable">
<tbody><tr>
<td>
<pre>FOR I = 1 TO N
xxx
NEXT I
</pre>
</td>
<td>
<pre><b>for</b> I&nbsp;:= 1 <b>to</b> N <b>do</b> <b>begin</b>
xxx
<b>end</b>;
</pre>
</td></tr>
<tr>
<td>
<pre>DO I = 1,N
xxx
END DO
</pre>
</td>
<td>
<pre><b>for</b> ( I=1; I&lt;=N; ++I ) {
xxx
}
</pre>
</td></tr></tbody></table>
<p>In these examples, if N &lt; 1 then the body of loop may execute once (with I having value 1) or not at all, depending on the programming language.
</p><p>In many programming languages, only integers can be reliably used in a count-controlled loop. Floating-point numbers are represented imprecisely due to hardware constraints, so a loop such as<br>
</p>
<pre> <b>for</b> X&nbsp;:= 0.1 <b>step</b> 0.1 <b>to</b> 1.0 <b>do</b>
</pre>
<p>might be repeated 9 or 10 times, depending on rounding errors and/or the hardware and/or the compiler version. Furthermore, if the increment of X occurs by repeated addition, accumulated rounding errors may mean that the value of X in each iteration can differ quite significantly from the expected sequence 0.1, 0.2, 0.3, ..., 1.0.
</p>
<div class="mw-heading mw-heading3"><h3 id="Condition-controlled_loops">Condition-controlled loops</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main articles: <a href="While_loop" title="While loop">While loop</a> and <a href="Do_while_loop" title="Do while loop">Do while loop</a></div>
<p>Most programming languages have constructions for repeating a loop until some condition changes. Some variations test the condition at the start of the loop; others test it at the end. If the test is at the start, the body may be skipped completely; if it is at the end, the body is always executed at least once.
</p>
<table class="wikitable">
<tbody><tr>
<td>
<pre>DO WHILE (test)
xxx
LOOP
</pre>
</td>
<td>
<pre><b>repeat</b>
xxx
<b>until</b> test;
</pre>
</td></tr>
<tr>
<td>
<pre><b>while</b> (test) {
xxx
}
</pre>
</td>
<td>
<pre><b>do</b>
xxx
<b>while</b> (test);
</pre>
</td></tr></tbody></table>
<p>A <a href="Control_break" title="Control break">control break</a> is a value change detection method used within ordinary loops to trigger processing for groups of values. Values are monitored within the loop and a change diverts program flow to the handling of the group event associated with them.
</p>
<pre> DO UNTIL (End-of-File)
IF new-zipcode &lt;&gt; current-zipcode
display_tally(current-zipcode, zipcount)
current-zipcode = new-zipcode
zipcount = 0
ENDIF
zipcount++
LOOP
</pre>
<div class="mw-heading mw-heading3"><h3 id="Collection-controlled_loops">Collection-controlled loops</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Foreach_loop" title="Foreach loop">Foreach</a></div>
<p>Several programming languages (e.g., <a href="Ada_(programming_language)" title="Ada (programming language)">Ada</a>, <a href="APL_(programming_language)" title="APL (programming language)">APL</a>, <a href="D_(programming_language)" title="D (programming language)">D</a>, <a href="C%2B%2B11" title="C++11">C++11</a>, <a href="Smalltalk" title="Smalltalk">Smalltalk</a>, <a href="PHP" title="PHP">PHP</a>, <a href="Perl" title="Perl">Perl</a>, <a href="Object_Pascal" title="Object Pascal">Object Pascal</a>, <a href="Java_(programming_language)" title="Java (programming language)">Java</a>, <a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a>, <a href="MATLAB" title="MATLAB">MATLAB</a>, <a href="Visual_Basic" title="Visual Basic">Visual Basic</a>, <a href="Ruby_(programming_language)" title="Ruby (programming language)">Ruby</a>, <a href="Python_(programming_language)" title="Python (programming language)">Python</a>, <a href="JavaScript" title="JavaScript">JavaScript</a>, <a href="Fortran_95" class="mw-redirect" title="Fortran 95">Fortran 95</a> and later) have special constructs which allow implicit looping through all elements of an array, or all members of a set or collection.
</p>
<pre> someCollection <b>do</b>: [:eachElement |xxx].

<b>for</b> Item <b>in</b> Collection <b>do</b> <b>begin</b> xxx <b>end</b>;

<b>foreach</b> (item; myCollection) { xxx }

<b>foreach</b> someArray { xxx }

<b>foreach</b> ($someArray as $k =&gt; $v) { xxx }

Collection&lt;String&gt; coll; <b>for</b> (String s&nbsp;: coll) {}

<b>foreach</b> (<b>string</b> s <b>in</b> myStringCollection) { xxx }

someCollection | ForEach-Object { $_ }

<b>forall</b> ( index = first:last:step... )
</pre>
<p><a href="Scala_(programming_language)" title="Scala (programming language)">Scala</a> has <a href="Scala_(programming_language)#For-expressions" title="Scala (programming language)">for-expressions</a>, which generalise collection-controlled loops, and also support other uses, such as <a href="Asynchronous_programming" class="mw-redirect" title="Asynchronous programming">asynchronous programming</a>. <a href="Haskell" title="Haskell">Haskell</a> has do-expressions and comprehensions, which together provide similar function to for-expressions in Scala.
</p>
<div class="mw-heading mw-heading3"><h3 id="General_iteration">General iteration</h3></div>
<p>General iteration constructs such as C's <code>for</code> statement and <a href="Common_Lisp" title="Common Lisp">Common Lisp</a>'s <code>do</code> form can be used to express any of the above sorts of loops, and others, such as looping over some number of collections in parallel. Where a more specific looping construct can be used, it is usually preferred over the general iteration construct, since it often makes the purpose of the expression clearer.
</p>
<div class="mw-heading mw-heading3"><h3 id="Infinite_loops">Infinite loops</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Infinite_loop" title="Infinite loop">Infinite loop</a></div>
<p><a href="Infinite_loop" title="Infinite loop">Infinite loops</a> are used to assure a program segment loops forever or until an exceptional condition arises, such as an error. For instance, an event-driven program (such as a <a href="Server_(computing)" title="Server (computing)">server</a>) should loop forever, handling events as they occur, only stopping when the process is terminated by an operator.
</p><p>Infinite loops can be implemented using other control flow constructs. Most commonly, in unstructured programming this is jump back up (goto), while in structured programming this is an indefinite loop (while loop) set to never end, either by omitting the condition or explicitly setting it to true, as <code>while (true) ...</code>. Some languages have special constructs for infinite loops, typically by omitting the condition from an indefinite loop. Examples include Ada (<code>loop ... end loop</code>),<sup id="cite_ref-9" class="reference"><a href="#cite_note-9"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup> Fortran (<code>DO ... END DO</code>), Go (<code>for { ... }</code>), and Ruby (<code>loop do ... end</code>).
</p><p>Often, an infinite loop is unintentionally created by a programming error in a condition-controlled loop, wherein the loop condition uses variables that never change within the loop.
</p>
<div class="mw-heading mw-heading3"><h3 id="Continuation_with_next_iteration">Continuation with next iteration</h3></div>
<p>Sometimes within the body of a loop there is a desire to skip the remainder of the loop body and continue with the next iteration of the loop. Some languages provide a statement such as <code>continue</code> (most languages), <code>skip</code>,<sup id="cite_ref-10" class="reference"><a href="#cite_note-10"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup> <code>cycle</code> (Fortran), or <code>next</code> (Perl and Ruby), which will do this. The effect is to prematurely terminate the innermost loop body and then resume as normal with the next iteration. If the iteration is the last one in the loop, the effect is to terminate the entire loop early.
</p>
<div class="mw-heading mw-heading3"><h3 id="Redo_current_iteration">Redo current iteration</h3></div>
<p>Some languages, like Perl<sup id="cite_ref-11" class="reference"><a href="#cite_note-11"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup> and Ruby,<sup id="cite_ref-12" class="reference"><a href="#cite_note-12"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup> have a <code>redo</code> statement that restarts the current iteration from the start.
</p>
<div class="mw-heading mw-heading3"><h3 id="Restart_loop">Restart loop</h3></div>
<p>Ruby has a <code>retry</code> statement that restarts the entire loop from the initial iteration.<sup id="cite_ref-13" class="reference"><a href="#cite_note-13"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Loop-and-a-half">Loop-and-a-half</h3></div>
<p>Common loop structures sometimes result in duplicated code, either repeated statements or repeated conditions. This arises for various reasons and has various proposed solutions to eliminate or minimize code duplication.<sup id="cite_ref-c2messy_14-0" class="reference"><a href="#cite_note-c2messy-14"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup> Other than the traditional unstructured solution of a <i>goto</i> statement,<sup id="cite_ref-FOOTNOTEKnuth1974278Simple_Iterations_15-0" class="reference"><a href="#cite_note-FOOTNOTEKnuth1974278Simple_Iterations-15"><span class="cite-bracket">[</span>14<span class="cite-bracket">]</span></a></sup> general structured solutions include having a conditional (<i>if</i> statement) inside the loop (possibly duplicating the condition but not the statements) or wrapping repeated logic in a function (so there is a duplicated function call, but the statements are not duplicated).<sup id="cite_ref-c2messy_14-1" class="reference"><a href="#cite_note-c2messy-14"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup>
</p><p>A common case is where the start of the loop is always executed, but the end may be skipped on the last iteration.<sup id="cite_ref-FOOTNOTEKnuth1974278Simple_Iterations_15-1" class="reference"><a href="#cite_note-FOOTNOTEKnuth1974278Simple_Iterations-15"><span class="cite-bracket">[</span>14<span class="cite-bracket">]</span></a></sup> This was dubbed by Dijkstra a loop which is performed "<i>n</i> and a half times",<sup id="cite_ref-16" class="reference"><a href="#cite_note-16"><span class="cite-bracket">[</span>15<span class="cite-bracket">]</span></a></sup> and is now called the <i><b>loop-and-a-half</b> problem</i>.<sup id="cite_ref-roberts_2-3" class="reference"><a href="#cite_note-roberts-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> Common cases include reading data in the first part, checking for end of data, and then processing the data in the second part; or processing, checking for end, and then preparing for the next iteration.<sup id="cite_ref-FOOTNOTEKnuth1974278Simple_Iterations_15-2" class="reference"><a href="#cite_note-FOOTNOTEKnuth1974278Simple_Iterations-15"><span class="cite-bracket">[</span>14<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-roberts_2-4" class="reference"><a href="#cite_note-roberts-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup> In these cases, the first part of the loop is executed <span class="nowrap">⁠<span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle n}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>n</mi>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle n}</annotation>
</semantics>
</math></span><img src="./a601995d55609f2d9f5e233e36fbe9ea26011b3b.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.338ex; width:1.395ex; height:1.676ex;" alt="{\displaystyle n}" loading="lazy"></span>⁠</span> times, but the second part is only executed <span class="nowrap">⁠<span class="mwe-math-element mwe-math-element-inline"><span class="mwe-math-mathml-inline mwe-math-mathml-a11y" style="display: none;"><math xmlns="http://www.w3.org/1998/Math/MathML" alttext="{\displaystyle n-1}">
<semantics>
<mrow class="MJX-TeXAtom-ORD">
<mstyle displaystyle="true" scriptlevel="0">
<mi>n</mi>
<mo>−<!-- − --></mo>
<mn>1</mn>
</mstyle>
</mrow>
<annotation encoding="application/x-tex">{\displaystyle n-1}</annotation>
</semantics>
</math></span><img src="./fbd0b0f32b28f51962943ee9ede4fb34198a2521.svg" class="mwe-math-fallback-image-inline mw-invert skin-invert" aria-hidden="true" style="vertical-align: -0.505ex; width:5.398ex; height:2.343ex;" alt="{\displaystyle n-1}" loading="lazy"></span>⁠</span> times.
</p><p>This problem has been recognized at least since 1967 by Knuth, with Wirth suggesting solving it via early loop exit.<sup id="cite_ref-FOOTNOTEKnuth1974279_17-0" class="reference"><a href="#cite_note-FOOTNOTEKnuth1974279-17"><span class="cite-bracket">[</span>16<span class="cite-bracket">]</span></a></sup> Since the 1990s this has been the most commonly taught solution, using a <i>break</i> statement, as in:<sup id="cite_ref-roberts_2-5" class="reference"><a href="#cite_note-roberts-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>
</p><p><br>
</p>
<pre><b>loop</b>
<i>statements</i>
<b>if</b> <i>condition</i> <b>break</b>
<i>statements</i>
<b>repeat</b>
</pre>
<p>A subtlety of this solution is that the condition is the <i>opposite</i> of a usual <i>while</i> condition: rewriting <b>while</b> <i>condition</i> ... <b>repeat</b> with an exit in the middle requires reversing the condition: <b>loop</b> ... <b>if not</b> <i>condition</i> <b>exit</b> ... <b>repeat</b>. The <a href="#Loop_with_test_in_the_middle">loop with test in the middle</a> control structure explicitly supports the loop-an-a-half use case, without reversing the condition.<sup id="cite_ref-FOOTNOTEKnuth1974279_17-1" class="reference"><a href="#cite_note-FOOTNOTEKnuth1974279-17"><span class="cite-bracket">[</span>16<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Early_exit_from_loops">Early exit from loops</h3></div>
<p>It is sometimes desirable to stop executing a loop before the end of the body; for example, when using a count-controlled loop to search through a table, one can stop as soon as the required item is found. Early exit is the most common way to solve the <a href="Loop-and-a-half" class="mw-redirect" title="Loop-and-a-half">loop-and-a-half</a> problem.<sup id="cite_ref-roberts_2-6" class="reference"><a href="#cite_note-roberts-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>
</p><p><br>
Some programming languages provide a statement such as <code>break</code> (most languages), <code>Exit</code> (Visual Basic), or <code>last</code> (Perl), which effect is to terminate the current loop immediately, and transfer control to the statement immediately after that loop.
</p><p>Other mechanisms for early exit include a <code>return</code> out of a subroutine executing the looped statements, breaking out of both the loop and the subroutine; and raising an exception. There are other <a href="#Proposed_control_structures">proposed control structures</a>, but they are not commonly implemented. This section treats early exit from only the loop ("break").
</p><p>Most commonly, this is used by combining a conditional (<b>if</b> statement) with an unconditional break, as in Ada:
</p>
<div class="mw-highlight mw-highlight-lang-ada mw-content-ltr" dir="ltr"><pre><span class="kr">loop</span>
<span class="n">Get</span><span class="p">(</span><span class="n">X</span><span class="p">);</span>
<span class="kr">if</span> <span class="n">X</span> <span class="o">=</span> <span class="mi">0</span> <span class="kr">then</span>
<span class="kr">exit</span><span class="p">;</span>
<span class="kr">end</span> <span class="kr">if</span><span class="p">;</span>
<span class="c1">-- Do something with X.</span>
<span class="kr">end</span> <span class="kr">loop</span><span class="p">;</span>
</pre></div>
<p>In this form, the condition is interpreted as <b>until</b>. Some languages, such as Ada, have syntax for a conditional break, here an <b>exit when</b> clause (not to be confused with the <b>exitwhen</b> statement in <a href="#Proposed_control_structures">§&nbsp;Proposed control structures</a>); if available, this is more idiomatic:
</p>
<div class="mw-highlight mw-highlight-lang-ada mw-content-ltr" dir="ltr"><pre><span class="kr">loop</span>
<span class="n">Get</span><span class="p">(</span><span class="n">X</span><span class="p">);</span>
<span class="kr">exit</span> <span class="kr">when</span> <span class="n">X</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="c1">-- Do something with X.</span>
<span class="kr">end</span> <span class="kr">loop</span><span class="p">;</span>
</pre></div>
<p>This functions similarly to a <a href="#Loop_with_test_in_the_middle">loop with test in the middle</a>, but in that case the test is part of the structure of the loop, dividing the body of the loop in half (visually indented at the same level as the start/end of loop), while early exit is unstructured: simply a statement that can appear anywhere in the body of the loop, and in fact multiple break statements are possible.
</p><p><a href="Python_(programming_language)" title="Python (programming language)">Python</a> supports conditional execution of code depending on whether a loop was exited early (with a <code>break</code> statement) or not by using an else-clause with the loop. For example,
</p>
<div class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><pre><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="n">set_of_numbers</span><span class="p">:</span>
<span class="k">if</span> <span class="n">isprime</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Set contains a prime number"</span><span class="p">)</span>
<span class="k">break</span>
<span class="k">else</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Set did not contain any prime numbers"</span><span class="p">)</span>
</pre></div>
<p>The <code>else</code> clause in the above example is linked to the <code>for</code> statement, and not the inner <code>if</code> statement. Both Python's <code>for</code> and <code>while</code> loops support such an else clause, which is executed only if early exit of the loop has not occurred.
</p>
<div class="mw-heading mw-heading4"><h4 id="Multi-level_breaks">Multi-level breaks</h4></div>
<p>Some languages support breaking out of nested loops; in theory circles, these are called <b>multi-level breaks</b>. One common use example is searching a multi-dimensional table. This can be done either via multilevel breaks (break out of <i>N</i> levels), as in bash<sup id="cite_ref-18" class="reference"><a href="#cite_note-18"><span class="cite-bracket">[</span>17<span class="cite-bracket">]</span></a></sup> and PHP,<sup id="cite_ref-19" class="reference"><a href="#cite_note-19"><span class="cite-bracket">[</span>18<span class="cite-bracket">]</span></a></sup> or via labeled breaks (break out and continue at given label), as in Ada. Go, Java and Perl.<sup id="cite_ref-20" class="reference"><a href="#cite_note-20"><span class="cite-bracket">[</span>19<span class="cite-bracket">]</span></a></sup> Alternatives to multilevel breaks include single breaks, together with a state variable which is tested to break out another level; exceptions, which are caught at the level being broken out to; placing the nested loops in a function and using return to effect termination of the entire nested loop; or using a label and a goto statement. C does not include a multilevel break, and the usual alternative is to use a goto to implement a labeled break.<sup id="cite_ref-21" class="reference"><a href="#cite_note-21"><span class="cite-bracket">[</span>20<span class="cite-bracket">]</span></a></sup> Python does not have a multilevel break or continue – this was proposed in <a rel="nofollow" class="external text" href="https://www.python.org/dev/peps/pep-3136/">PEP 3136</a>, and rejected on the basis that the added complexity was not worth the rare legitimate use.<sup id="cite_ref-22" class="reference"><a href="#cite_note-22"><span class="cite-bracket">[</span>21<span class="cite-bracket">]</span></a></sup>
</p><p>The notion of multi-level breaks is of some interest in <a href="Theoretical_computer_science" title="Theoretical computer science">theoretical computer science</a>, because it gives rise to what is today called the <i>Kosaraju hierarchy</i>.<sup id="cite_ref-kozen_23-0" class="reference"><a href="#cite_note-kozen-23"><span class="cite-bracket">[</span>22<span class="cite-bracket">]</span></a></sup> In 1973 <a href="S._Rao_Kosaraju" title="S. Rao Kosaraju">S. Rao Kosaraju</a> refined the <a href="Structured_program_theorem" title="Structured program theorem">structured program theorem</a> by proving that it is possible to avoid adding additional variables in structured programming, as long as arbitrary-depth, multi-level breaks from loops are allowed.<sup id="cite_ref-24" class="reference"><a href="#cite_note-24"><span class="cite-bracket">[</span>23<span class="cite-bracket">]</span></a></sup> Furthermore, Kosaraju proved that a strict hierarchy of programs exists: for every integer <i>n</i>, there exists a program containing a multi-level break of depth <i>n</i> that cannot be rewritten as a program with multi-level breaks of depth less than <i>n</i> without introducing added variables.<sup id="cite_ref-kozen_23-1" class="reference"><a href="#cite_note-kozen-23"><span class="cite-bracket">[</span>22<span class="cite-bracket">]</span></a></sup>
</p><p>In his 2004 textbook, <a href="David_Watt_(computer_scientist)" title="David Watt (computer scientist)">David Watt</a> uses Tennent's notion of <a href="S-algol" title="S-algol">sequencer</a> to explain the similarity between multi-level breaks and return statements. Watt notes that a class of sequencers known as <i>escape sequencers</i>, defined as "sequencer that terminates execution of a textually enclosing command or procedure", encompasses both breaks from loops (including multi-level breaks) and return statements. As commonly implemented, however, return sequencers may also carry a (return) value, whereas the break sequencer as implemented in contemporary languages usually cannot.<sup id="cite_ref-WattFindlay2004b_25-0" class="reference"><a href="#cite_note-WattFindlay2004b-25"><span class="cite-bracket">[</span>24<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading3"><h3 id="Loop_with_test_in_the_middle">Loop with test in the middle</h3></div>
<p>The following structure was proposed by <a href="Ole-Johan_Dahl" title="Ole-Johan Dahl">Dahl</a> in 1972:<sup id="cite_ref-26" class="reference"><a href="#cite_note-26"><span class="cite-bracket">[</span>25<span class="cite-bracket">]</span></a></sup>
</p>
<pre> <b>loop</b> <b>loop</b>
xxx1 read(char);
<b>while</b> test; <b>while</b> <b>not</b> atEndOfFile;
xxx2 write(char);
<b>repeat</b>; <b>repeat</b>;
</pre>
<p>The construction here can be thought of as a <b>do</b> loop with the <b>while</b> check in the middle, which allows clear <a href="#Loop-and-a-half">loop-and-a-half</a> logic. Further, by omitting individual components, this single construction can replace several constructions in most programming languages. If <i>xxx1</i> is omitted, we get a loop with the test at the top (a traditional <b>while</b> loop). If <i>xxx2</i> is omitted, we get a loop with the test at the bottom, equivalent to a <b>do while</b> loop in many languages. If <b>while</b> is omitted, we get an infinite loop. This construction also allows keeping the same polarity of the condition even when in the middle, unlike early exit, which requires reversing the polarity (adding a <b>not</b>),<sup id="cite_ref-FOOTNOTEKnuth1974279_17-2" class="reference"><a href="#cite_note-FOOTNOTEKnuth1974279-17"><span class="cite-bracket">[</span>16<span class="cite-bracket">]</span></a></sup> functioning as <b>until</b> instead of <b>while</b>.
</p><p>This structure is not widely supported, with most languages instead using <b>if</b> ... <b>break</b> for conditional early exit.
</p><p>This is supported by some languages, such as <a href="Forth_(programming_language)" title="Forth (programming language)">Forth</a>, where the syntax is BEGIN ... WHILE ... REPEAT,<sup id="cite_ref-27" class="reference"><a href="#cite_note-27"><span class="cite-bracket">[</span>26<span class="cite-bracket">]</span></a></sup> and the <a href="Shell_script" title="Shell script">shell script</a> languages <a href="Bourne_shell" title="Bourne shell">Bourne shell</a> (<code>sh</code>) and <a href="Bash_(Unix_shell)" title="Bash (Unix shell)">bash</a>, where the syntax is <b>while</b> ... <b>do</b> ... <b>done</b> or <b>until</b> ... <b>do</b> ... <b>done</b>, as:<sup id="cite_ref-28" class="reference"><a href="#cite_note-28"><span class="cite-bracket">[</span>27<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-29" class="reference"><a href="#cite_note-29"><span class="cite-bracket">[</span>28<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-highlight mw-highlight-lang-bash mw-content-ltr" dir="ltr"><pre><span class="k">while</span>
<span class="w"> </span>statement-1
<span class="w"> </span>statement-2
<span class="w"> </span>...
<span class="w"> </span>condition
<span class="k">do</span>
<span class="w"> </span>statement-a
<span class="w"> </span>statement-b
<span class="w"> </span>...
<span class="k">done</span>
</pre></div>
<p>The shell syntax works because the <b>while</b> (or <b>until</b>) loop accepts a list of commands as a condition,<sup id="cite_ref-30" class="reference"><a href="#cite_note-30"><span class="cite-bracket">[</span>29<span class="cite-bracket">]</span></a></sup> formally:
</p>
<pre> <b>while</b> <i>test-commands</i>; <b>do</b> <i>consequent-commands</i>; <b>done</b>
</pre>
<p>The value (exit status) of the list of <i>test-commands</i> is the value of the <i>last</i> command, and these can be separated by newlines, resulting in the idiomatic form above.
</p><p>Similar constructions are possible in C and C++ with the <a href="Comma_operator" title="Comma operator">comma operator</a>, and <a href="Comma_operator#Other_languages" title="Comma operator">other languages with similar constructs</a>, which allow shoehorning a list of statements into the while condition:
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="k">while</span><span class="w"> </span><span class="p">(</span>
<span class="w"> </span><span class="n">statement_1</span><span class="p">,</span>
<span class="w"> </span><span class="n">statement_2</span><span class="p">,</span>
<span class="w"> </span><span class="n">condition</span>
<span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">statement_a</span><span class="p">;</span>
<span class="w"> </span><span class="n">statement_b</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>While legal, this is marginal, and it is primarily used, if at all, only for short modify-then-test cases, as in:<sup id="cite_ref-31" class="reference"><a href="#cite_note-31"><span class="cite-bracket">[</span>30<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-highlight mw-highlight-lang-c mw-content-ltr" dir="ltr"><pre><span class="k">while</span><span class="w"> </span><span class="p">(</span><span class="n">read_string</span><span class="p">(</span><span class="n">s</span><span class="p">),</span><span class="w"> </span><span class="n">strlen</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="mi">0</span><span class="p">)</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Loop_variants_and_invariants">Loop variants and invariants</h3></div>
<p><a href="Loop_variant" title="Loop variant">Loop variants</a> and <a href="Loop_invariant" title="Loop invariant">loop invariants</a> are used to express correctness of loops.<sup id="cite_ref-32" class="reference"><a href="#cite_note-32"><span class="cite-bracket">[</span>31<span class="cite-bracket">]</span></a></sup>
</p><p>In practical terms, a loop variant is an integer expression which has an initial non-negative value. The variant's value must decrease during each loop iteration but must never become negative during the correct execution of the loop. Loop variants are used to guarantee that loops will terminate.
</p><p>A loop invariant is an assertion which must be true before the first loop iteration and remain true after each iteration. This implies that when a loop terminates correctly, both the exit condition and the loop invariant are satisfied. Loop invariants are used to monitor specific properties of a loop during successive iterations.
</p><p>Some programming languages, such as <a href="Eiffel_(programming_language)" title="Eiffel (programming language)">Eiffel</a> contain native support for loop variants and invariants. In other cases, support is an add-on, such as the <a href="Java_Modeling_Language" title="Java Modeling Language">Java Modeling Language</a>'s specification for <a rel="nofollow" class="external text" href="http://www.eecs.ucf.edu/~leavens/JML//jmlrefman/jmlrefman_12.html#SEC168">loop statements</a> in <a href="Java_(programming_language)" title="Java (programming language)">Java</a>.
</p>
<div class="mw-heading mw-heading3"><h3 id="Loop_sublanguage">Loop sublanguage</h3></div>
<p>Some <a href="Lisp_(programming_language)" title="Lisp (programming language)">Lisp</a> dialects provide an extensive sublanguage for describing Loops. An early example can be found in Conversional Lisp of <a href="Interlisp" title="Interlisp">Interlisp</a>. <a href="Common_Lisp" title="Common Lisp">Common Lisp</a><sup id="cite_ref-33" class="reference"><a href="#cite_note-33"><span class="cite-bracket">[</span>32<span class="cite-bracket">]</span></a></sup> provides a Loop macro which implements such a sublanguage.
</p>
<div class="mw-heading mw-heading3"><h3 id="Loop_system_cross-reference_table">Loop system cross-reference table</h3></div>
<table class="wikitable">

<tbody><tr>
<th rowspan="2"><a href="Programming_language" title="Programming language">Programming language</a>
</th>
<th colspan="3">conditional
</th>
<th colspan="4">loop
</th>
<th rowspan="2">early exit
</th>
<th rowspan="2">loop continuation
</th>
<th rowspan="2">redo
</th>
<th rowspan="2">retry
</th>
<th colspan="2">correctness facilities
</th></tr>
<tr>
<th>begin
</th>
<th>middle
</th>
<th>end
</th>
<th>count
</th>
<th>collection
</th>
<th>general
</th>
<th>infinite <style data-mw-deduplicate="TemplateStyles:r1041539562">
/* start https://en.wikipedia.org/ */


.mw-parser-output .citation{word-wrap:break-word}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}


/* end https://en.wikipedia.org/ */
</style><sup class="citation nobold" id="ref_loop_infinitea"><a href="#endnote_loop_infinitea">[1]</a></sup>
</th>
<th>variant
</th>
<th>invariant
</th></tr>
<tr>
<td><a href="Ada_(programming_language)" title="Ada (programming language)">Ada</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">arrays
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="APL_(programming_language)" title="APL (programming language)">APL</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">deep nested <sup class="citation nobold" id="ref_deep_break_ca"><a href="#endnote_deep_break_ca">[3]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="C_(programming_language)" title="C (programming language)">C</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No <sup class="citation nobold" id="ref_loop_fora"><a href="#endnote_loop_fora">[2]</a></sup>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">deep nested <sup class="citation nobold" id="ref_deep_break_ca"><a href="#endnote_deep_break_ca">[3]</a></sup>
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">deep nested <sup class="citation nobold" id="ref_deep_break_ca"><a href="#endnote_deep_break_ca">[3]</a></sup>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="C%2B%2B" title="C++">C++</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No <sup class="citation nobold" id="ref_loop_forb"><a href="#endnote_loop_forb">[2]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes <sup class="citation nobold" id="ref_loop_foreacha"><a href="#endnote_loop_foreacha">[9]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">deep nested <sup class="citation nobold" id="ref_deep_break_cb"><a href="#endnote_deep_break_cb">[3]</a></sup>
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">deep nested <sup class="citation nobold" id="ref_deep_break_ca"><a href="#endnote_deep_break_ca">[3]</a></sup>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No <sup class="citation nobold" id="ref_loop_forc"><a href="#endnote_loop_forc">[2]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">deep nested <sup class="citation nobold" id="ref_deep_break_cc"><a href="#endnote_deep_break_cc">[3]</a></sup>
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">deep nested <sup class="citation nobold" id="ref_deep_break_ca"><a href="#endnote_deep_break_ca">[3]</a></sup>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="COBOL" title="COBOL">COBOL</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">deep nested <sup class="citation nobold" id="ref_cobol_deep_exita"><a href="#endnote_cobol_deep_exita">[15]</a></sup>
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">deep nested <sup class="citation nobold" id="ref_cobol_deep_exita"><a href="#endnote_cobol_deep_exita">[14]</a></sup>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="Common_Lisp" title="Common Lisp">Common Lisp</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">builtin only <sup class="citation nobold" id="ref_cl_sequence_typea"><a href="#endnote_cl_sequence_typea">[16]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="D_(programming_language)" title="D (programming language)">D</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes<sup class="citation nobold" id="ref_DInfinitea"><a href="#endnote_DInfinitea">[14]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="Eiffel_(programming_language)" title="Eiffel (programming language)">Eiffel</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes <sup class="citation nobold" id="ref_count_loop_eiffela"><a href="#endnote_count_loop_eiffela">[10]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">one level <sup class="citation nobold" id="ref_count_loop_eiffela"><a href="#endnote_count_loop_eiffela">[10]</a></sup>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No <sup class="citation nobold" id="ref_retry_in_eiffela"><a href="#endnote_retry_in_eiffela">[11]</a></sup>
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">integer only <sup class="citation nobold" id="ref_integer_varianta"><a href="#endnote_integer_varianta">[13]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td></tr>
<tr>
<td><a href="F_Sharp_(programming_language)" title="F Sharp (programming language)">F#</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No <sup class="citation nobold" id="ref_deep_break_eb"><a href="#endnote_deep_break_eb">[6]</a></sup>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="FORTRAN_77" class="mw-redirect" title="FORTRAN 77">FORTRAN 77</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">one level
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="Fortran_90" class="mw-redirect" title="Fortran 90">Fortran 90</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="Fortran_95" class="mw-redirect" title="Fortran 95">Fortran 95</a> and later
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">arrays
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="Go_(programming_language)" title="Go (programming language)">Go</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">builtin only
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="Haskell" title="Haskell">Haskell</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No <sup class="citation nobold" id="ref_deep_break_ea"><a href="#endnote_deep_break_ea">[6]</a></sup>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="Java_(programming_language)" title="Java (programming language)">Java</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No <sup class="citation nobold" id="ref_loop_ford"><a href="#endnote_loop_ford">[2]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">non-native <sup class="citation nobold" id="ref_requires_JMLa"><a href="#endnote_requires_JMLa">[12]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">non-native <sup class="citation nobold" id="ref_requires_JMLa"><a href="#endnote_requires_JMLa">[12]</a></sup>
</td></tr>
<tr>
<td><a href="JavaScript" title="JavaScript">JavaScript</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No <sup class="citation nobold" id="ref_loop_forg"><a href="#endnote_loop_forg">[2]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td>Natural
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td></tr>
<tr>
<td><a href="OCaml" title="OCaml">OCaml</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">arrays,lists
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No <sup class="citation nobold" id="ref_deep_break_eb"><a href="#endnote_deep_break_eb">[6]</a></sup>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="PHP" title="PHP">PHP</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No <sup class="citation nobold" id="ref_loop_fore"><a href="#endnote_loop_fore">[2]</a></sup> <sup class="citation nobold" id="ref_loop_forb"><a href="#endnote_loop_forb">[5]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes <sup class="citation nobold" id="ref_loop_phpforeacha"><a href="#endnote_loop_phpforeacha">[4]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="Perl" title="Perl">Perl</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No <sup class="citation nobold" id="ref_loop_forf"><a href="#endnote_loop_forf">[2]</a></sup> <sup class="citation nobold" id="ref_loop_forc"><a href="#endnote_loop_forc">[5]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">deep nested
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="Python_(programming_language)" title="Python (programming language)">Python</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No <sup class="citation nobold" id="ref_loop_rangea"><a href="#endnote_loop_rangea">[5]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">deep nested <sup class="citation nobold" id="ref_deep_break_ec"><a href="#endnote_deep_break_ec">[6]</a></sup>
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">deep nested <sup class="citation nobold" id="ref_deep_break_ec"><a href="#endnote_deep_break_ec">[6]</a></sup>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="Rebol" title="Rebol">Rebol</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No <sup class="citation nobold" id="ref_whilea"><a href="#endnote_whilea">[7]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No <sup class="citation nobold" id="ref_usera"><a href="#endnote_usera">[8]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">one level <sup class="citation nobold" id="ref_deep_break_ed"><a href="#endnote_deep_break_ed">[6]</a></sup>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="Ruby_(programming_language)" title="Ruby (programming language)">Ruby</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">deep nested <sup class="citation nobold" id="ref_deep_break_ee"><a href="#endnote_deep_break_ee">[6]</a></sup>
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">deep nested <sup class="citation nobold" id="ref_deep_break_ee"><a href="#endnote_deep_break_ee">[6]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="Standard_ML" title="Standard ML">Standard ML</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">arrays,lists
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No <sup class="citation nobold" id="ref_deep_break_eb"><a href="#endnote_deep_break_eb">[6]</a></sup>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="Visual_Basic_.NET" class="mw-redirect" title="Visual Basic .NET">Visual Basic .NET</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">one level per type of loop
</td>
<td style="background:#FFB; color:black;vertical-align:middle;text-align:center;" class="table-partial">one level per type of loop
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr>
<tr>
<td><a href="PowerShell" title="PowerShell">PowerShell</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No <sup class="citation nobold" id="ref_loop_forh"><a href="#endnote_loop_forh">[2]</a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td></tr></tbody></table>
<ol><li><span class="citation wikicite" id="endnote_loop_infinitea"><a href="#ref_loop_infinitea"><b><sup>a</sup></b></a></span> <code>while (true)</code> does not count as an infinite loop for this purpose, because it is not a dedicated language structure.</li>
<li><span class="citation wikicite" id="endnote_loop_fora"><a href="#ref_loop_fora"><b><sup>a</sup></b></a></span> <span class="citation wikicite" id="endnote_loop_forb"><a href="#ref_loop_forb"><b><sup>b</sup></b></a></span> <span class="citation wikicite" id="endnote_loop_forc"><a href="#ref_loop_forc"><b><sup>c</sup></b></a></span> <span class="citation wikicite" id="endnote_loop_ford"><a href="#ref_loop_ford"><b><sup>d</sup></b></a></span> <span class="citation wikicite" id="endnote_loop_fore"><a href="#ref_loop_fore"><b><sup>e</sup></b></a></span> <span class="citation wikicite" id="endnote_loop_forf"><a href="#ref_loop_forf"><b><sup>f</sup></b></a></span> <span class="citation wikicite" id="endnote_loop_forg"><a href="#ref_loop_forg"><b><sup>g</sup></b></a></span> <span class="citation wikicite" id="endnote_loop_forh"><a href="#ref_loop_forh"><b><sup>h</sup></b></a></span> C's <code>for (<i>init</i>; <i>test</i>; <i>increment</i>)</code> loop is a general loop construct, not specifically a counting one, although it is often used for that.</li>
<li><span class="citation wikicite" id="endnote_deep_break_ca"><a href="#ref_deep_break_ca"><b><sup>a</sup></b></a></span> <span class="citation wikicite" id="endnote_deep_break_cb"><a href="#ref_deep_break_cb"><b><sup>b</sup></b></a></span> <span class="citation wikicite" id="endnote_deep_break_cc"><a href="#ref_deep_break_cc"><b><sup>c</sup></b></a></span> Deep breaks may be accomplished in APL, C, C++ and C# through the use of labels and gotos.</li>
<li><span class="citation wikicite" id="endnote_loop_phpforeacha"><a href="#ref_loop_phpforeacha"><b><sup>a</sup></b></a></span> Iteration over objects was <a rel="nofollow" class="external text" href="http://www.php.net/manual/en/language.oop5.iterations.php">added</a> in PHP 5.</li>
<li><span class="citation wikicite" id="endnote_loop_rangea"><a href="#ref_loop_rangea"><b><sup>a</sup></b></a></span> <span class="citation wikicite" id="endnote_loop_rangeb"><a href="#ref_loop_rangeb"><b><sup>b</sup></b></a></span> <span class="citation wikicite" id="endnote_loop_rangec"><a href="#ref_loop_rangec"><b><sup>c</sup></b></a></span> A counting loop can be simulated by iterating over an incrementing list or generator, for instance, Python's <code>range()</code>.</li>
<li><span class="citation wikicite" id="endnote_deep_break_ea"><a href="#ref_deep_break_ea"><b><sup>a</sup></b></a></span> <span class="citation wikicite" id="endnote_deep_break_eb"><a href="#ref_deep_break_eb"><b><sup>b</sup></b></a></span> <span class="citation wikicite" id="endnote_deep_break_ec"><a href="#ref_deep_break_ec"><b><sup>c</sup></b></a></span> <span class="citation wikicite" id="endnote_deep_break_ed"><a href="#ref_deep_break_ed"><b><sup>d</sup></b></a></span> <span class="citation wikicite" id="endnote_deep_break_ee"><a href="#ref_deep_break_ee"><b><sup>e</sup></b></a></span> Deep breaks may be accomplished through the use of exception handling.</li>
<li><span class="citation wikicite" id="endnote_whilea"><a href="#ref_whilea"><b><sup>a</sup></b></a></span> There is no special construct, since the <code>while</code> function can be used for this.</li>
<li><span class="citation wikicite" id="endnote_usera"><a href="#ref_usera"><b><sup>a</sup></b></a></span> There is no special construct, but users can define general loop functions.</li>
<li><span class="citation wikicite" id="endnote_loop_foreacha"><a href="#ref_loop_foreacha"><b><sup>a</sup></b></a></span> The <a href="C%2B%2B11" title="C++11">C++11</a> standard introduced the <a href="C%2B%2B11#Range-based_for_loop" title="C++11">range-based for</a>. In the <a href="Standard_Template_Library" title="Standard Template Library">STL</a>, there is a <code>std::for_each</code> <a href="Template_(programming)" class="mw-redirect" title="Template (programming)">template</a> function which can iterate on STL <a href="Container_(data_structure)" class="mw-redirect" title="Container (data structure)">containers</a> and call a <a href="Unary_function" title="Unary function">unary function</a> for each element.<sup id="cite_ref-34" class="reference"><a href="#cite_note-34"><span class="cite-bracket">[</span>33<span class="cite-bracket">]</span></a></sup> The functionality also can be constructed as <a href="C_preprocessor#Macro_definition_and_expansion" title="C preprocessor">macro</a> on these containers.<sup id="cite_ref-35" class="reference"><a href="#cite_note-35"><span class="cite-bracket">[</span>34<span class="cite-bracket">]</span></a></sup></li>
<li><span class="citation wikicite" id="endnote_count_loop_eiffela"><a href="#ref_count_loop_eiffela"><b><sup>a</sup></b></a></span> Count-controlled looping is effected by iteration across an integer interval; early exit by including an additional condition for exit.</li>
<li><span class="citation wikicite" id="endnote_retry_in_eiffela"><a href="#ref_retry_in_eiffela"><b><sup>a</sup></b></a></span> Eiffel supports a reserved word <code>retry</code>, however it is used in <a href="Exception_handling#Exception_handling_based_on_design_by_contract" title="Exception handling">exception handling</a>, not loop control.</li>
<li><span class="citation wikicite" id="endnote_requires_JMLa"><a href="#ref_requires_JMLa"><b><sup>a</sup></b></a></span> Requires <a href="Java_Modeling_Language" title="Java Modeling Language">Java Modeling Language</a> (JML) behavioral interface specification language.</li>
<li><span class="citation wikicite" id="endnote_integer_varianta"><a href="#ref_integer_varianta"><b><sup>a</sup></b></a></span> Requires loop variants to be integers; transfinite variants are not supported. <a rel="nofollow" class="external text" href="http://archive.eiffel.com/doc/faq/variant.html">Eiffel: Why loop variants are integers</a></li>
<li><span class="citation wikicite" id="endnote_DInfinitea"><a href="#ref_DInfinitea"><b><sup>a</sup></b></a></span> D supports infinite collections, and the ability to iterate over those collections. This does not require any special construct.</li>
<li><span class="citation wikicite" id="endnote_cobol_deep_exita"><a href="#ref_cobol_deep_exita"><b><sup>a</sup></b></a></span> Deep breaks can be achieved using <code>GO TO</code> and procedures.</li>
<li><span class="citation wikicite" id="endnote_cl_sequence_typea"><a href="#ref_cl_sequence_typea"><b><sup>a</sup></b></a></span> Common Lisp predates the concept of generic collection type.</li></ol>
<div class="mw-heading mw-heading2"><h2 id="Structured_non-local_control_flow">Structured non-local control flow</h2></div>
<p>Many programming languages, especially those favoring more dynamic styles of programming, offer constructs for <i>non-local control flow</i>. These cause the flow of execution to jump out of a given context and resume at some <a href="Predeclared" title="Predeclared">predeclared</a> point. <i><a href="Exception_handling#Condition_systems" title="Exception handling">Conditions</a></i>, <i><a href="Exception_handling" title="Exception handling">exceptions</a></i> and <i><a href="Continuation" title="Continuation">continuations</a></i> are three common sorts of non-local control constructs; more exotic ones also exist, such as <a href="Generator_(computer_programming)" title="Generator (computer programming)">generators</a>, <a href="Coroutine" title="Coroutine">coroutines</a> and the <a href="Futures_and_promises" title="Futures and promises">async</a> keyword.
</p>
<div class="mw-heading mw-heading3"><h3 id="Conditions">Conditions</h3></div>
<p>The earliest <a href="Fortran" title="Fortran">Fortran</a> compilers had statements for testing exceptional conditions. These included the <code>IF ACCUMULATOR OVERFLOW</code>, <code>IF QUOTIENT OVERFLOW</code>, and <code>IF DIVIDE CHECK</code> statements. In the interest of machine independence, they were not included in FORTRAN IV and the Fortran 66 Standard. However since Fortran 2003 it is possible to test for numerical issues via calls to functions in the <code>IEEE_EXCEPTIONS</code> module.
</p><p><a href="PL/I" title="PL/I">PL/I</a> has some 22 standard conditions (e.g., ZERODIVIDE SUBSCRIPTRANGE ENDFILE) which can be raised and which can be intercepted by: ON <i>condition</i> action; Programmers can also define and use their own named conditions.
</p><p>Like the <i>unstructured if</i>, only one statement can be specified so in many cases a GOTO is needed to decide where flow of control should resume.
</p><p>Unfortunately, some implementations had a substantial overhead in both space and time (especially SUBSCRIPTRANGE), so many programmers tried to avoid using conditions.
</p><p>Common Syntax examples:
</p>
<pre> <b>ON</b> <i>condition</i> <b>GOTO</b> <i>label</i>
</pre>
<div class="mw-heading mw-heading3"><h3 id="Exceptions">Exceptions</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Exception_handling" title="Exception handling">Exception handling</a></div>
<p>Modern languages have a specialized structured construct for exception handling which does not rely on the use of <code>GOTO</code> or (multi-level) breaks or returns. For example, in C++ one can write:
</p>
<div class="mw-highlight mw-highlight-lang-cpp mw-content-ltr" dir="ltr"><pre><span class="k">try</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">xxx1</span><span class="w"> </span><span class="c1">// Somewhere in here</span>
<span class="w"> </span><span class="n">xxx2</span><span class="w"> </span><span class="c1">// use: '''throw''' someValue;</span>
<span class="w"> </span><span class="n">xxx3</span>
<span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">someClass</span><span class="o">&amp;</span><span class="w"> </span><span class="n">someId</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// catch value of someClass</span>
<span class="w"> </span><span class="n">actionForSomeClass</span><span class="w"> </span>
<span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(</span><span class="n">someType</span><span class="o">&amp;</span><span class="w"> </span><span class="n">anotherId</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// catch value of someType</span>
<span class="w"> </span><span class="n">actionForSomeType</span>
<span class="p">}</span><span class="w"> </span><span class="k">catch</span><span class="w"> </span><span class="p">(...)</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="c1">// catch anything not already caught</span>
<span class="w"> </span><span class="n">actionForAnythingElse</span>
<span class="p">}</span>
</pre></div>
<p>Any number and variety of <code>catch</code> clauses can be used above. If there is no <code>catch</code> matching a particular <code>throw</code>, control percolates back through subroutine calls and/or nested blocks until a matching <code>catch</code> is found or until the end of the main program is reached, at which point the program is forcibly stopped with a suitable error message.
</p><p>Via C++'s influence, <code>catch</code> is the keyword reserved for declaring a pattern-matching exception handler in other languages popular today, like Java or C#. Some other languages like Ada use the keyword <code>exception</code> to introduce an exception handler and then may even employ a different keyword (<code>when</code> in Ada) for the pattern matching. A few languages like <a href="AppleScript" title="AppleScript">AppleScript</a> incorporate placeholders in the exception handler syntax to automatically extract several pieces of information when the exception occurs. This approach is exemplified below by the <code>on error</code> construct from AppleScript:
</p>
<div class="mw-highlight mw-highlight-lang-applescript mw-content-ltr" dir="ltr"><pre><span class="k">try</span>
<span class="k">set</span> <span class="nv">myNumber</span> <span class="k">to</span> <span class="nv">myNumber</span> <span class="o">/</span> <span class="mi">0</span>
<span class="k">on</span> <span class="k">error</span> <span class="nv">e</span> <span class="nv">number</span> <span class="nv">n</span> <span class="k">from</span> <span class="nv">f</span> <span class="k">to</span> <span class="nv">t</span> <span class="nv">partial</span> <span class="no">result</span> <span class="nv">pr</span>
<span class="k">if</span> <span class="p">(</span> <span class="nv">e</span> <span class="o">=</span> <span class="s2">"Can't divide by zero"</span> <span class="p">)</span> <span class="k">then</span> <span class="nb">display dialog</span> <span class="s2">"You must not do that"</span>
<span class="k">end</span> <span class="k">try</span>
</pre></div>
<p>David Watt's 2004 textbook also analyzes exception handling in the framework of sequencers (introduced in this article in the section on early exits from loops). Watt notes that an abnormal situation, generally exemplified with arithmetic overflows or <a href="Input/output" title="Input/output">input/output</a> failures like file not found, is a kind of error that "is detected in some low-level program unit, but [for which] a handler is more naturally located in a high-level program unit". For example, a program might contain several calls to read files, but the action to perform when a file is not found depends on the meaning (purpose) of the file in question to the program and thus a handling routine for this abnormal situation cannot be located in low-level system code. Watts further notes that introducing status flags testing in the caller, as single-exit structured programming or even (multi-exit) return sequencers would entail, results in a situation where "the application code tends to get cluttered by tests of status flags" and that "the programmer might forgetfully or lazily omit to test a status flag. In fact, abnormal situations represented by status flags are by default ignored!" Watt notes that in contrast to status flags testing, exceptions have the opposite <a href="Default_(computer_science)" title="Default (computer science)">default behavior</a>, causing the program to terminate unless the program deals with the exception explicitly in some way, possibly by adding explicit code to ignore it. Based on these arguments, Watt concludes that jump sequencers or escape sequencers are less suitable as a dedicated exception sequencer with the semantics discussed above.<sup id="cite_ref-36" class="reference"><a href="#cite_note-36"><span class="cite-bracket">[</span>35<span class="cite-bracket">]</span></a></sup>
</p><p>In Object Pascal, D, Java, C#, and Python a <code>finally</code> clause can be added to the <code>try</code> construct. No matter how control leaves the <code>try</code> the code inside the <code>finally</code> clause is guaranteed to execute. This is useful when writing code that must relinquish an expensive resource (such as an opened file or a database connection) when finished processing:
</p>
<div class="mw-highlight mw-highlight-lang-csharp mw-content-ltr" dir="ltr"><pre><span class="n">FileStream</span><span class="w"> </span><span class="n">stm</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">null</span><span class="p">;</span><span class="w"> </span><span class="c1">// C# example</span>
<span class="k">try</span>
<span class="p">{</span>
<span class="w"> </span><span class="n">stm</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">FileStream</span><span class="p">(</span><span class="s">"logfile.txt"</span><span class="p">,</span><span class="w"> </span><span class="n">FileMode</span><span class="p">.</span><span class="n">Create</span><span class="p">);</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nf">ProcessStuff</span><span class="p">(</span><span class="n">stm</span><span class="p">);</span><span class="w"> </span><span class="c1">// may throw an exception</span>
<span class="p">}</span><span class="w"> </span>
<span class="k">finally</span>
<span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">stm</span><span class="w"> </span><span class="o">!=</span><span class="w"> </span><span class="k">null</span><span class="p">)</span>
<span class="w"> </span><span class="n">stm</span><span class="p">.</span><span class="n">Close</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
<p>Since this pattern is fairly common, C# has a special syntax:
</p>
<div class="mw-highlight mw-highlight-lang-csharp mw-content-ltr" dir="ltr"><pre><span class="k">using</span><span class="w"> </span><span class="p">(</span><span class="kt">var</span><span class="w"> </span><span class="n">stm</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">FileStream</span><span class="p">(</span><span class="s">"logfile.txt"</span><span class="p">,</span><span class="w"> </span><span class="n">FileMode</span><span class="p">.</span><span class="n">Create</span><span class="p">))</span>
<span class="p">{</span>
<span class="w"> </span><span class="k">return</span><span class="w"> </span><span class="nf">ProcessStuff</span><span class="p">(</span><span class="n">stm</span><span class="p">);</span><span class="w"> </span><span class="c1">// may throw an exception</span>
<span class="p">}</span>
</pre></div>
<p>Upon leaving the <code>using</code>-block, the compiler guarantees that the <code>stm</code> object is released, effectively <a href="Name_binding" title="Name binding">binding</a> the variable to the file stream while abstracting from the side effects of initializing and releasing the file. Python's <code>with</code> statement and Ruby's block argument to <code>File.open</code> are used to similar effect.
</p><p>All the languages mentioned above define standard exceptions and the circumstances under which they are thrown. Users can throw exceptions of their own; C++ allows users to throw and catch almost any type, including basic types like <code>int</code>, whereas other languages like Java are less permissive.
</p>
<div class="mw-heading mw-heading3"><h3 id="Continuations">Continuations</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Continuation" title="Continuation">Continuation</a></div>
<div class="mw-heading mw-heading3"><h3 id="Async">Async</h3></div>
<p>C# 5.0 introduced the async keyword for supporting <a href="Asynchronous_I/O" title="Asynchronous I/O">asynchronous I/O</a> in a "direct style".
</p>
<div class="mw-heading mw-heading3"><h3 id="Generators">Generators</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Generator_(computer_science)" class="mw-redirect" title="Generator (computer science)">Generator (computer science)</a></div>
<p><a href="Generator_(computer_science)" class="mw-redirect" title="Generator (computer science)">Generators</a>, also known as semicoroutines, allow control to be yielded to a consumer method temporarily, typically using a <code class="mw-highlight mw-highlight-lang-python mw-content-ltr" dir="ltr"><span class="k">yield</span></code> keyword (<a rel="nofollow" class="external text" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/yield">yield description</a>) . Like the async keyword, this supports programming in a "direct style".
</p>
<div class="mw-heading mw-heading3"><h3 id="Coroutines">Coroutines</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="Coroutine" title="Coroutine">Coroutine</a></div>
<p><a href="Coroutines" class="mw-redirect" title="Coroutines">Coroutines</a> are functions that can yield control to each other - a form of <a href="Co-operative_multitasking" class="mw-redirect" title="Co-operative multitasking">co-operative multitasking</a> without threads.
</p><p>Coroutines can be implemented as a library if the programming language provides either continuations or generators - so the distinction between coroutines and generators in practice is a technical detail.
</p>
<div class="mw-heading mw-heading3"><h3 id="Non-local_control_flow_cross_reference">Non-local control flow cross reference</h3></div>
<table class="wikitable">

<tbody><tr>
<th><a href="Programming_language" title="Programming language">Programming language</a>
</th>
<th>conditions
</th>
<th>exceptions
</th>
<th>generators/coroutines
</th>
<th>async
</th></tr>
<tr>
<td><a href="Ada_(programming_language)" title="Ada (programming language)">Ada</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background: var(--background-color-interactive, #EEE); color: var(--color-base, black); vertical-align: middle; white-space: nowrap; text-align: center;" class="table-Un­known">?
</td>
<td style="background: var(--background-color-interactive, #EEE); color: var(--color-base, black); vertical-align: middle; white-space: nowrap; text-align: center;" class="table-Un­known">?
</td></tr>
<tr>
<td><a href="APL_(programming_language)" title="APL (programming language)">APL</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td></tr>
<tr>
<td><a href="C_(programming_language)" title="C (programming language)">C</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td></tr>
<tr>
<td><a href="C%2B%2B" title="C++">C++</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background: var(--background-color-interactive, #EEE); color: var(--color-base, black); vertical-align: middle; white-space: nowrap; text-align: center;" class="table-Un­known">?
</td></tr>
<tr>
<td><a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td></tr>
<tr>
<td><a href="COBOL" title="COBOL">COBOL</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td></tr>
<tr>
<td><a href="Common_Lisp" title="Common Lisp">Common Lisp</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background: var(--background-color-interactive, #EEE); color: var(--color-base, black); vertical-align: middle; white-space: nowrap; text-align: center;" class="table-Un­known">?
</td>
<td style="background: var(--background-color-interactive, #EEE); color: var(--color-base, black); vertical-align: middle; white-space: nowrap; text-align: center;" class="table-Un­known">?
</td></tr>
<tr>
<td><a href="D_(programming_language)" title="D (programming language)">D</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background: var(--background-color-interactive, #EEE); color: var(--color-base, black); vertical-align: middle; white-space: nowrap; text-align: center;" class="table-Un­known">?
</td></tr>
<tr>
<td><a href="Eiffel_(programming_language)" title="Eiffel (programming language)">Eiffel</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background: var(--background-color-interactive, #EEE); color: var(--color-base, black); vertical-align: middle; white-space: nowrap; text-align: center;" class="table-Un­known">?
</td>
<td style="background: var(--background-color-interactive, #EEE); color: var(--color-base, black); vertical-align: middle; white-space: nowrap; text-align: center;" class="table-Un­known">?
</td></tr>
<tr>
<td><a href="Erlang_(programming_language)" title="Erlang (programming language)">Erlang</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background: var(--background-color-interactive, #EEE); color: var(--color-base, black); vertical-align: middle; white-space: nowrap; text-align: center;" class="table-Un­known">?
</td></tr>
<tr>
<td><a href="F_Sharp_(programming_language)" title="F Sharp (programming language)">F#</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td></tr>
<tr>
<td><a href="Go_(programming_language)" title="Go (programming language)">Go</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background: var(--background-color-interactive, #EEE); color: var(--color-base, black); vertical-align: middle; white-space: nowrap; text-align: center;" class="table-Un­known">?
</td></tr>
<tr>
<td><a href="Haskell" title="Haskell">Haskell</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td></tr>
<tr>
<td><a href="Java_(programming_language)" title="Java (programming language)">Java</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td></tr>
<tr>
<td><a href="JavaScript" title="JavaScript">JavaScript</a>
</td>
<td style="background: var(--background-color-interactive, #EEE); color: var(--color-base, black); vertical-align: middle; white-space: nowrap; text-align: center;" class="table-Un­known">?
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td></tr>
<tr>
<td><a href="Objective-C" title="Objective-C">Objective-C</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background: var(--background-color-interactive, #EEE); color: var(--color-base, black); vertical-align: middle; white-space: nowrap; text-align: center;" class="table-Un­known">?
</td></tr>
<tr>
<td><a href="PHP" title="PHP">PHP</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background: var(--background-color-interactive, #EEE); color: var(--color-base, black); vertical-align: middle; white-space: nowrap; text-align: center;" class="table-Un­known">?
</td></tr>
<tr>
<td><a href="PL/I" title="PL/I">PL/I</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td></tr>
<tr>
<td><a href="Python_(programming_language)" title="Python (programming language)">Python</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes<sup id="cite_ref-37" class="reference"><a href="#cite_note-37"><span class="cite-bracket">[</span>36<span class="cite-bracket">]</span></a></sup>
</td></tr>
<tr>
<td><a href="Rebol" title="Rebol">Rebol</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background: var(--background-color-interactive, #EEE); color: var(--color-base, black); vertical-align: middle; white-space: nowrap; text-align: center;" class="table-Un­known">?
</td></tr>
<tr>
<td><a href="Ruby_(programming_language)" title="Ruby (programming language)">Ruby</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">via extension<sup id="cite_ref-38" class="reference"><a href="#cite_note-38"><span class="cite-bracket">[</span>37<span class="cite-bracket">]</span></a></sup>
</td></tr>
<tr>
<td><a href="Rust_(programming_language)" title="Rust (programming language)">Rust</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">experimental <sup id="cite_ref-39" class="reference"><a href="#cite_note-39"><span class="cite-bracket">[</span>38<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-40" class="reference"><a href="#cite_note-40"><span class="cite-bracket">[</span>39<span class="cite-bracket">]</span></a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes<sup id="cite_ref-41" class="reference"><a href="#cite_note-41"><span class="cite-bracket">[</span>40<span class="cite-bracket">]</span></a></sup>
</td></tr>
<tr>
<td><a href="Scala_(programming_language)" title="Scala (programming language)">Scala</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">via experimental extension<sup id="cite_ref-42" class="reference"><a href="#cite_note-42"><span class="cite-bracket">[</span>41<span class="cite-bracket">]</span></a></sup>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">via experimental extension
</td></tr>
<tr>
<td><a href="Tcl" title="Tcl">Tcl</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">via traces
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">via event loop
</td></tr>
<tr>
<td><a href="Visual_Basic_.NET" class="mw-redirect" title="Visual Basic .NET">Visual Basic .NET</a>
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background: var(--background-color-interactive, #EEE); color: var(--color-base, black); vertical-align: middle; white-space: nowrap; text-align: center;" class="table-Un­known">?
</td></tr>
<tr>
<td><a href="PowerShell" title="PowerShell">PowerShell</a>
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background:#9EFF9E;color:black;vertical-align:middle;text-align:center;" class="table-yes">Yes
</td>
<td style="background:#FFC7C7;color:black;vertical-align:middle;text-align:center;" class="table-no">No
</td>
<td style="background: var(--background-color-interactive, #EEE); color: var(--color-base, black); vertical-align: middle; white-space: nowrap; text-align: center;" class="table-Un­known">?
</td></tr></tbody></table>
<div class="mw-heading mw-heading2"><h2 id="Proposed_control_structures">Proposed control structures</h2></div>
<div class="mw-heading mw-heading3"><h3 id="COMEFROM">COMEFROM</h3></div>
<div role="note" class="hatnote navigation-not-searchable">Main article: <a href="COMEFROM" title="COMEFROM">COMEFROM</a></div>
<p>In a spoof <a href="Datamation" title="Datamation">Datamation</a> article<sup id="cite_ref-43" class="reference"><a href="#cite_note-43"><span class="cite-bracket">[</span>42<span class="cite-bracket">]</span></a></sup> in 1973, R. Lawrence Clark suggested that the GOTO statement could be replaced by the <a href="COMEFROM" title="COMEFROM">COMEFROM</a> statement, and provides some entertaining examples. COMEFROM was implemented in one <a href="Esoteric_programming_language" title="Esoteric programming language">esoteric programming language</a> named <a href="INTERCAL" title="INTERCAL">INTERCAL</a>.
</p>
<div class="mw-heading mw-heading3"><h3 id="Event-based_early_exit_from_nested_loops">Event-based early exit from nested loops</h3></div>
<p><a href="Zahn's_construct" title="Zahn's construct">This construct</a> was proposed by <a href="Zahn's_construct" title="Zahn's construct">Zahn</a> in 1974,<sup id="cite_ref-44" class="reference"><a href="#cite_note-44"><span class="cite-bracket">[</span>43<span class="cite-bracket">]</span></a></sup> and discussed in <a href="#CITEREFKnuth1974">Knuth (1974)</a>. A modified version is presented here.
</p>
<pre> <b>exitwhen</b> EventA <b>or</b> EventB <b>or</b> EventC;
xxx
<b>exits</b>
EventA: actionA
EventB: actionB
EventC: actionC
<b>endexit</b>;
</pre>
<p><b>exitwhen</b> is used to specify the events which may occur within <i>xxx</i>,
their occurrence is indicated by using the name of the event as a statement. When some event does occur, the relevant action is carried out, and then control passes just after <b>endexit</b>. This construction provides a very clear separation between determining that some situation applies, and the action to be taken for that situation.
</p><p><b>exitwhen</b> is conceptually similar to <a href="Exception_handling" title="Exception handling">exception handling</a>, and exceptions or similar constructs are used for this purpose in many languages.
</p><p>The following simple example involves searching a two-dimensional table for a particular item.
</p>
<pre> <b>exitwhen</b> found <b>or</b> missing;
<b>for</b> I&nbsp;:= 1 <b>to</b> N <b>do</b>
<b>for</b> J&nbsp;:= 1 <b>to</b> M <b>do</b>
<b>if</b> table[I,J] = target <b>then</b> found;
missing;
<b>exits</b>
found: print ("item is in table");
missing: print ("item is not in table");
<b>endexit</b>;
</pre>
<div class="mw-heading mw-heading2"><h2 id="Security">Security</h2></div>
<p>One way to attack a piece of software is to redirect the flow of execution of a program. A variety of <a href="Control-flow_integrity" title="Control-flow integrity">control-flow integrity</a> techniques, including <a href="Stack_canary" class="mw-redirect" title="Stack canary">stack canaries</a>, <a href="Buffer_overflow_protection" title="Buffer overflow protection">buffer overflow protection</a>, shadow stacks, and <a href="Virtual_method_table" title="Virtual method table">vtable</a> pointer verification, are used to defend against these attacks.<sup id="cite_ref-45" class="reference"><a href="#cite_note-45"><span class="cite-bracket">[</span>44<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-46" class="reference"><a href="#cite_note-46"><span class="cite-bracket">[</span>45<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-47" class="reference"><a href="#cite_note-47"><span class="cite-bracket">[</span>46<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1184024115">
/* start https://en.wikipedia.org/ */


.mw-parser-output .div-col{margin-top:0.3em;column-width:30em}.mw-parser-output .div-col-small{font-size:90%}.mw-parser-output .div-col-rules{column-rule:1px solid #aaa}.mw-parser-output .div-col dl,.mw-parser-output .div-col ol,.mw-parser-output .div-col ul{margin-top:0}.mw-parser-output .div-col li,.mw-parser-output .div-col dd{page-break-inside:avoid;break-inside:avoid-column}


/* end https://en.wikipedia.org/ */
</style><div class="div-col">
<ul><li><a href="Branch_(computer_science)" title="Branch (computer science)">Branch (computer science)</a></li>
<li><a href="Control_flow_analysis" class="mw-redirect" title="Control flow analysis">Control-flow analysis</a></li>
<li><a href="Control-flow_diagram" title="Control-flow diagram">Control-flow diagram</a></li>
<li><a href="Control-flow_graph" title="Control-flow graph">Control-flow graph</a></li>
<li><a href="Control_table" title="Control table">Control table</a></li>
<li><a href="Coroutine" title="Coroutine">Coroutine</a></li>
<li><a href="Cyclomatic_complexity" title="Cyclomatic complexity">Cyclomatic complexity</a></li>
<li><a href="DRAKON" title="DRAKON">Drakon-chart</a></li>
<li><a href="Flowchart" title="Flowchart">Flowchart</a></li>
<li><a href="Goto" title="Goto">Goto</a></li>
<li><a href="Jeroo" title="Jeroo">Jeroo</a>, helps learn control structures</li>
<li><a href="Main_loop" class="mw-redirect" title="Main loop">Main loop</a></li>
<li><a href="Predication_(computer_architecture)" title="Predication (computer architecture)">predication</a></li>
<li><a href="Recursion" title="Recursion">Recursion</a></li>
<li><a href="Scheduling_(computing)" title="Scheduling (computing)">Scheduling (computing)</a></li>
<li><a href="Spaghetti_code" title="Spaghetti code">Spaghetti code</a></li>
<li><a href="Structured_programming" title="Structured programming">Structured programming</a></li>
<li><a href="Subroutine" class="mw-redirect" title="Subroutine">Subroutine</a></li>
<li><a href="Switch_statement" title="Switch statement">Switch statement</a>, alters control flow conditionally</li>
<li><a href="Zahn's_construct" title="Zahn's construct">Zahn's construct</a></li></ul>
</div>
<div class="mw-heading mw-heading2"><h2 id="Notes">Notes</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */


.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}


/* end https://en.wikipedia.org/ */
</style><div class="reflist reflist-lower-alpha">
<div class="mw-references-wrap"><ol class="references">
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text">In Fortran, this statement was deemed obsolescent in Fortran-90, and deleted as of Fortran 2018.</span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<div class="reflist">
<div class="mw-references-wrap mw-references-columns"><ol class="references">
<li id="cite_note-1"><span class="mw-cite-backlink"><b><a href="#cite_ref-1">^</a></b></span> <span class="reference-text">Böhm, Jacopini. "Flow diagrams, turing machines and languages with only two formation rules" <a href="Comm._ACM" class="mw-redirect" title="Comm. ACM">Comm. ACM</a>, 9(5):366-371, May 1966.</span>
</li>
<li id="cite_note-roberts-2"><span class="mw-cite-backlink">^ <a href="#cite_ref-roberts_2-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-roberts_2-1"><sup><i><b>b</b></i></sup></a> <a href="#cite_ref-roberts_2-2"><sup><i><b>c</b></i></sup></a> <a href="#cite_ref-roberts_2-3"><sup><i><b>d</b></i></sup></a> <a href="#cite_ref-roberts_2-4"><sup><i><b>e</b></i></sup></a> <a href="#cite_ref-roberts_2-5"><sup><i><b>f</b></i></sup></a> <a href="#cite_ref-roberts_2-6"><sup><i><b>g</b></i></sup></a></span> <span class="reference-text">Roberts, E. [1995] "<a rel="nofollow" class="external text" href="http://cs.stanford.edu/people/eroberts/papers/SIGCSE-1995/LoopExits.pdf">Loop Exits and Structured Programming: Reopening the Debate</a> <a rel="nofollow" class="external text" href="https://web.archive.org/web/20140725130816/http://cs.stanford.edu/people/eroberts/papers/SIGCSE-1995/LoopExits.pdf">Archived</a> 2014-07-25 at the <a href="Wayback_Machine" title="Wayback Machine">Wayback Machine</a>," ACM SIGCSE Bulletin, (27)1: 268–272.</span>
</li>
<li id="cite_note-WattFindlay2004-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-WattFindlay2004_3-0">^</a></b></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */


.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}


/* end https://en.wikipedia.org/ */
</style><cite id="CITEREFDavid_Anthony_WattWilliam_Findlay2004" class="citation book cs1">David Anthony Watt; William Findlay (2004). <i>Programming language design concepts</i>. John Wiley &amp; Sons. p.&nbsp;228. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a>&nbsp;<bdi>978-0-470-85320-7</bdi>.</cite></span>
</li>
<li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://www.geeksforgeeks.org/nested-loops-in-c-with-examples/">"Nested Loops in C with Examples"</a>. <i>GeeksforGeeks</i>. 2019-11-25<span class="reference-accessdate">. Retrieved <span class="nowrap">2024-03-14</span></span>.</cite></span>
</li>
<li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://www.w3schools.com/python/gloss_python_for_nested.asp">"Python Nested Loops"</a>. <i>www.w3schools.com</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2024-03-14</span></span>.</cite></span>
</li>
<li id="cite_note-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-7">^</a></b></span> <span class="reference-text"><cite id="CITEREFDean2019" class="citation web cs1">Dean, Jenna (2019-11-22). <a rel="nofollow" class="external text" href="https://medium.com/swlh/nested-loops-ee1dbb9fc8ab">"Nested Loops"</a>. <i>The Startup</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2024-03-14</span></span>.</cite></span>
</li>
<li id="cite_note-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-8">^</a></b></span> <span class="reference-text"><cite id="CITEREFKnuth1974" class="citation journal cs1"><a href="Donald_Knuth" title="Donald Knuth">Knuth, Donald E.</a> (1974). "Structured Programming with <code>go to</code> Statements". <i><a href="ACM_Computing_Surveys" title="ACM Computing Surveys">Computing Surveys</a></i>. <b>6</b> (4): <span class="nowrap">261–</span>301. <a href="CiteSeerX_(identifier)" class="mw-redirect" title="CiteSeerX (identifier)">CiteSeerX</a>&nbsp;<span class="id-lock-free" title="Freely accessible"><a rel="nofollow" class="external text" href="https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.103.6084">10.1.1.103.6084</a></span>. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1145%2F356635.356640">10.1145/356635.356640</a>. <a href="S2CID_(identifier)" class="mw-redirect" title="S2CID (identifier)">S2CID</a>&nbsp;<a rel="nofollow" class="external text" href="https://api.semanticscholar.org/CorpusID:207630080">207630080</a>.</cite></span>
</li>
<li id="cite_note-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-9">^</a></b></span> <span class="reference-text"><a href="https://en.wikibooks.org/wiki/Ada_Programming/Control#Endless_Loop" class="extiw external" title="b:Ada Programming/Control">Ada Programming: Control: Endless Loop</a></span>
</li>
<li id="cite_note-10"><span class="mw-cite-backlink"><b><a href="#cite_ref-10">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20200728081722/http://www.megacpptutorials.com/2012/12/what-is-loop.html">"What is a loop and how we can use them?"</a>. Archived from <a rel="nofollow" class="external text" href="http://www.megacpptutorials.com/2012/12/what-is-loop.html">the original</a> on 2020-07-28<span class="reference-accessdate">. Retrieved <span class="nowrap">2020-05-25</span></span>.</cite></span>
</li>
<li id="cite_note-11"><span class="mw-cite-backlink"><b><a href="#cite_ref-11">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://perldoc.perl.org/functions/redo.html">"redo - perldoc.perl.org"</a>. <i>perldoc.perl.org</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2020-09-25</span></span>.</cite></span>
</li>
<li id="cite_note-12"><span class="mw-cite-backlink"><b><a href="#cite_ref-12">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.ruby-lang.org/en/2.4.0/syntax/control_expressions_rdoc.html">"control_expressions - Documentation for Ruby 2.4.0"</a>. <i>docs.ruby-lang.org</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2020-09-25</span></span>.</cite></span>
</li>
<li id="cite_note-13"><span class="mw-cite-backlink"><b><a href="#cite_ref-13">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.ruby-lang.org/en/2.3.0/syntax/control_expressions_rdoc.html">"control_expressions - Documentation for Ruby 2.3.0"</a>. <i>docs.ruby-lang.org</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2020-09-25</span></span>.</cite></span>
</li>
<li id="cite_note-c2messy-14"><span class="mw-cite-backlink">^ <a href="#cite_ref-c2messy_14-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-c2messy_14-1"><sup><i><b>b</b></i></sup></a></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://wiki.c2.com/?MessyLoopConditions">"Messy Loop Conditions"</a>. <i>WikiWikiWeb</i>. 2014-11-03.</cite></span>
</li>
<li id="cite_note-FOOTNOTEKnuth1974278Simple_Iterations-15"><span class="mw-cite-backlink">^ <a href="#cite_ref-FOOTNOTEKnuth1974278Simple_Iterations_15-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-FOOTNOTEKnuth1974278Simple_Iterations_15-1"><sup><i><b>b</b></i></sup></a> <a href="#cite_ref-FOOTNOTEKnuth1974278Simple_Iterations_15-2"><sup><i><b>c</b></i></sup></a></span> <span class="reference-text"><a href="#CITEREFKnuth1974">Knuth 1974</a>, p.&nbsp;278, Simple Iterations.</span>
</li>
<li id="cite_note-16"><span class="mw-cite-backlink"><b><a href="#cite_ref-16">^</a></b></span> <span class="reference-text"><a href="Edsger_W._Dijkstra" title="Edsger W. Dijkstra">Edsger W. Dijkstra</a>, personal communication to <a href="Donald_Knuth" title="Donald Knuth">Donald Knuth</a> on 1974-01-03, cited in <a href="#CITEREFKnuth1974">Knuth (1974</a>, p.&nbsp;278, Simple Iterations)</span>
</li>
<li id="cite_note-FOOTNOTEKnuth1974279-17"><span class="mw-cite-backlink">^ <a href="#cite_ref-FOOTNOTEKnuth1974279_17-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-FOOTNOTEKnuth1974279_17-1"><sup><i><b>b</b></i></sup></a> <a href="#cite_ref-FOOTNOTEKnuth1974279_17-2"><sup><i><b>c</b></i></sup></a></span> <span class="reference-text"><a href="#CITEREFKnuth1974">Knuth 1974</a>, p.&nbsp;279.</span>
</li>
<li id="cite_note-18"><span class="mw-cite-backlink"><b><a href="#cite_ref-18">^</a></b></span> <span class="reference-text">Advanced Bash Scripting Guide: <a rel="nofollow" class="external text" href="http://tldp.org/LDP/abs/html/loopcontrol.html">11.3. Loop Control</a></span>
</li>
<li id="cite_note-19"><span class="mw-cite-backlink"><b><a href="#cite_ref-19">^</a></b></span> <span class="reference-text">PHP Manual: "<a rel="nofollow" class="external text" href="http://php.net/manual/en/control-structures.break.php">break</a>"</span>
</li>
<li id="cite_note-20"><span class="mw-cite-backlink"><b><a href="#cite_ref-20">^</a></b></span> <span class="reference-text">perldoc: <a rel="nofollow" class="external text" href="http://perldoc.perl.org/functions/last.html">last</a></span>
</li>
<li id="cite_note-21"><span class="mw-cite-backlink"><b><a href="#cite_ref-21">^</a></b></span> <span class="reference-text">comp.lang.c FAQ list · "<a rel="nofollow" class="external text" href="http://c-faq.com/misc/multibreak.html">Question 20.20b</a>"</span>
</li>
<li id="cite_note-22"><span class="mw-cite-backlink"><b><a href="#cite_ref-22">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://mail.python.org/pipermail/python-3000/2007-July/008663.html">[Python-3000] Announcing PEP 3136</a>, Guido van Rossum</span>
</li>
<li id="cite_note-kozen-23"><span class="mw-cite-backlink">^ <a href="#cite_ref-kozen_23-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-kozen_23-1"><sup><i><b>b</b></i></sup></a></span> <span class="reference-text"><cite id="CITEREFKozen2008" class="citation book cs1">Kozen, Dexter (2008). "The Böhm–Jacopini Theorem is False, Propositionally". <a rel="nofollow" class="external text" href="http://www.cs.cornell.edu/~kozen/papers/BohmJacopini.pdf"><i>Mathematics of Program Construction</i></a> <span class="cs1-format">(PDF)</span>. Lecture Notes in Computer Science. Vol.&nbsp;5133. pp.&nbsp;<span class="nowrap">177–</span>192. <a href="CiteSeerX_(identifier)" class="mw-redirect" title="CiteSeerX (identifier)">CiteSeerX</a>&nbsp;<span class="id-lock-free" title="Freely accessible"><a rel="nofollow" class="external text" href="https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.218.9241">10.1.1.218.9241</a></span>. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1007%2F978-3-540-70594-9_11">10.1007/978-3-540-70594-9_11</a>. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a>&nbsp;<bdi>978-3-540-70593-2</bdi>.</cite></span>
</li>
<li id="cite_note-24"><span class="mw-cite-backlink"><b><a href="#cite_ref-24">^</a></b></span> <span class="reference-text">Kosaraju, S. Rao. "Analysis of structured programs," Proc. Fifth Annual ACM Syrup.
Theory of Computing, (May 1973), 240-252; also in J. Computer and System Sciences, 9,
3 (December 1974), cited by <a href="#CITEREFKnuth1974">Knuth (1974)</a>.</span>
</li>
<li id="cite_note-WattFindlay2004b-25"><span class="mw-cite-backlink"><b><a href="#cite_ref-WattFindlay2004b_25-0">^</a></b></span> <span class="reference-text"><cite id="CITEREFDavid_Anthony_WattWilliam_Findlay2004" class="citation book cs1">David Anthony Watt; William Findlay (2004). <i>Programming language design concepts</i>. John Wiley &amp; Sons. pp.&nbsp;<span class="nowrap">215–</span>221. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a>&nbsp;<bdi>978-0-470-85320-7</bdi>.</cite></span>
</li>
<li id="cite_note-26"><span class="mw-cite-backlink"><b><a href="#cite_ref-26">^</a></b></span> <span class="reference-text">Dahl &amp; Dijkstra &amp; Hoare, "Structured Programming" Academic Press, 1972.</span>
</li>
<li id="cite_note-27"><span class="mw-cite-backlink"><b><a href="#cite_ref-27">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://www.forth.com/starting-forth/6-forth-do-loops/">"6. Throw It For a Loop"</a>.</cite></span>
</li>
<li id="cite_note-28"><span class="mw-cite-backlink"><b><a href="#cite_ref-28">^</a></b></span> <span class="reference-text"><cite class="citation cs2"><a rel="nofollow" class="external text" href="https://www.gnu.org/software/bash/manual/bash.html#Looping-Constructs-1">"3.2.5.1 Looping Constructs"</a>, <a rel="nofollow" class="external text" href="https://www.gnu.org/software/bash/manual/bash.html"><i>The GNU Bash Reference Manual</i></a>, 2025-05-18</cite></span>
</li>
<li id="cite_note-29"><span class="mw-cite-backlink"><b><a href="#cite_ref-29">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://langdev.stackexchange.com/questions/1815/how-could-a-language-make-the-loop-and-a-half-less-error-prone/1868#1868">"How could a language make the loop-and-a-half less error-prone?"</a>. <i>Stack Exchange: Programming Language Design and Implementation</i>.</cite></span>
</li>
<li id="cite_note-30"><span class="mw-cite-backlink"><b><a href="#cite_ref-30">^</a></b></span> <span class="reference-text"><cite class="citation cs2"><a rel="nofollow" class="external text" href="https://www.gnu.org/software/bash/manual/bash.html#Lists">"3.2.4 Lists of Commands"</a>, <a rel="nofollow" class="external text" href="https://www.gnu.org/software/bash/manual/bash.html"><i>The GNU Bash Reference Manual</i></a>, 2025-05-18</cite></span>
</li>
<li id="cite_note-31"><span class="mw-cite-backlink"><b><a href="#cite_ref-31">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://stackoverflow.com/questions/52550/what-does-the-comma-operator-do/52615#52615">"What does the comma operator , do?"</a>. <i><a href="Stack_Overflow" title="Stack Overflow">Stack Overflow</a></i>.</cite></span>
</li>
<li id="cite_note-32"><span class="mw-cite-backlink"><b><a href="#cite_ref-32">^</a></b></span> <span class="reference-text"><cite id="CITEREFMeyer1991" class="citation book cs1">Meyer, Bertrand (1991). <i>Eiffel: The Language</i>. Prentice Hall. pp.&nbsp;<span class="nowrap">129–</span>131.</cite></span>
</li>
<li id="cite_note-33"><span class="mw-cite-backlink"><b><a href="#cite_ref-33">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.lispworks.com/documentation/HyperSpec/Body/m_loop.htm">"Common Lisp LOOP macro"</a>.</cite></span>
</li>
<li id="cite_note-34"><span class="mw-cite-backlink"><b><a href="#cite_ref-34">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://www.sgi.com/tech/stl/for_each.html">for_each</a>. Sgi.com. Retrieved on 2010-11-09.</span>
</li>
<li id="cite_note-35"><span class="mw-cite-backlink"><b><a href="#cite_ref-35">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="https://boost-sandbox.sourceforge.net/libs/foreach/doc/html/">Chapter 1. Boost.Foreach</a> <a rel="nofollow" class="external text" href="https://web.archive.org/web/20100129070613/http://boost-sandbox.sourceforge.net/libs/foreach/doc/html/">Archived</a> 2010-01-29 at the <a href="Wayback_Machine" title="Wayback Machine">Wayback Machine</a>. Boost-sandbox.sourceforge.net (2009-12-19). Retrieved on 2010-11-09.</span>
</li>
<li id="cite_note-36"><span class="mw-cite-backlink"><b><a href="#cite_ref-36">^</a></b></span> <span class="reference-text"><cite id="CITEREFDavid_Anthony_WattWilliam_Findlay2004" class="citation book cs1">David Anthony Watt; William Findlay (2004). <i>Programming language design concepts</i>. John Wiley &amp; Sons. pp.&nbsp;<span class="nowrap">221–</span>222. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a>&nbsp;<bdi>978-0-470-85320-7</bdi>.</cite></span>
</li>
<li id="cite_note-37"><span class="mw-cite-backlink"><b><a href="#cite_ref-37">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.python.org/3/library/asyncio.html">"asyncio — Asynchronous I/O"</a>. <i>Python documentation</i>.</cite></span>
</li>
<li id="cite_note-38"><span class="mw-cite-backlink"><b><a href="#cite_ref-38">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://github.com/socketry/async">"Socketry/Async"</a>. <i><a href="GitHub" title="GitHub">GitHub</a></i>. 25 February 2022.</cite></span>
</li>
<li id="cite_note-39"><span class="mw-cite-backlink"><b><a href="#cite_ref-39">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://doc.rust-lang.org/beta/unstable-book/language-features/generators.html">"generators - The Rust Unstable Book"</a>. <i>doc.rust-lang.org</i>.</cite></span>
</li>
<li id="cite_note-40"><span class="mw-cite-backlink"><b><a href="#cite_ref-40">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.rs/corona/0.4.3/corona/">"corona - Rust"</a>. <i>docs.rs</i>.</cite></span>
</li>
<li id="cite_note-41"><span class="mw-cite-backlink"><b><a href="#cite_ref-41">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://rust-lang.github.io/async-book/">"Introduction - Asynchronous Programming in Rust"</a>. <i>rust-lang.github.io</i>.</cite></span>
</li>
<li id="cite_note-42"><span class="mw-cite-backlink"><b><a href="#cite_ref-42">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://storm-enroute.com/coroutines/">"Jitsi Meet"</a>. Storm-enroute.com<span class="reference-accessdate">. Retrieved <span class="nowrap">2022-09-07</span></span>.</cite></span>
</li>
<li id="cite_note-43"><span class="mw-cite-backlink"><b><a href="#cite_ref-43">^</a></b></span> <span class="reference-text"><a rel="nofollow" class="external text" href="http://www.fortran.com/fortran/come_from.html">We don't know where to GOTO if we don't know where we've COME FROM. This (spoof) linguistic innovation lives up to all expectations.</a> <a rel="nofollow" class="external text" href="https://web.archive.org/web/20180716171336/http://www.fortran.com/fortran/come_from.html">Archived</a> 2018-07-16 at the <a href="Wayback_Machine" title="Wayback Machine">Wayback Machine</a> By R. Lawrence Clark* From Datamation, December, 1973</span>
</li>
<li id="cite_note-44"><span class="mw-cite-backlink"><b><a href="#cite_ref-44">^</a></b></span> <span class="reference-text">Zahn, C. T. "A control statement for natural top-down structured programming" presented at Symposium on Programming Languages, Paris, 1974.</span>
</li>
<li id="cite_note-45"><span class="mw-cite-backlink"><b><a href="#cite_ref-45">^</a></b></span> <span class="reference-text"><cite id="CITEREFPayerKuznetsov" class="citation web cs1"><a href="Mathias_Payer" title="Mathias Payer">Payer, Mathias</a>; Kuznetsov, Volodymyr. <a rel="nofollow" class="external text" href="https://nebelwelt.net/blog/20141007-CFICPSCPIdiffs.html">"On differences between the CFI, CPS, and CPI properties"</a>. <i>nebelwelt.net</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2016-06-01</span></span>.</cite></span>
</li>
<li id="cite_note-46"><span class="mw-cite-backlink"><b><a href="#cite_ref-46">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.darkreading.com/vulnerabilities---threats/adobe-flash-bug-discovery-leads-to-new-attack-mitigation-method/d/d-id/1323092">"Adobe Flash Bug Discovery Leads To New Attack Mitigation Method"</a>. <i>Dark Reading</i>. 10 November 2015<span class="reference-accessdate">. Retrieved <span class="nowrap">2016-06-01</span></span>.</cite></span>
</li>
<li id="cite_note-47"><span class="mw-cite-backlink"><b><a href="#cite_ref-47">^</a></b></span> <span class="reference-text"><cite id="CITEREFEndgame" class="citation pressrelease cs1">Endgame. <a rel="nofollow" class="external text" href="http://www.prnewswire.com/news-releases/endgame-to-present-at-black-hat-usa-2016-300267060.html">"Endgame to Present at Black Hat USA 2016"</a>. <i>www.prnewswire.com</i> (Press release)<span class="reference-accessdate">. Retrieved <span class="nowrap">2016-06-01</span></span>.</cite></span>
</li>
</ol></div></div>
<div class="mw-heading mw-heading2"><h2 id="Further_reading">Further reading</h2></div>
<ul><li>Hoare, C. A. R. "Partition: Algorithm 63," "Quicksort: Algorithm 64," and "Find: Algorithm 65." Comm. ACM 4, 321–322, 1961.</li></ul>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1290876196">
/* start https://en.wikipedia.org/ */


.mw-parser-output .side-box{margin:4px 0;box-sizing:border-box;border:1px solid #aaa;font-size:88%;line-height:1.25em;background-color:var(--background-color-interactive-subtle,#f8f9fa);display:flow-root}.mw-parser-output .infobox .side-box{font-size:100%}.mw-parser-output .side-box-abovebelow,.mw-parser-output .side-box-text{padding:0.25em 0.9em}.mw-parser-output .side-box-image{padding:2px 0 2px 0.9em;text-align:center}.mw-parser-output .side-box-imageright{padding:2px 0.9em 2px 0;text-align:center}@media(min-width:500px){.mw-parser-output .side-box-flex{display:flex;align-items:center}.mw-parser-output .side-box-text{flex:1;min-width:0}}@media(min-width:720px){.mw-parser-output .side-box{width:238px}.mw-parser-output .side-box-right{clear:right;float:right;margin-left:1em}.mw-parser-output .side-box-left{margin-right:1em}}


/* end https://en.wikipedia.org/ */
</style><style data-mw-deduplicate="TemplateStyles:r1237033735">
/* start https://en.wikipedia.org/ */


@media print{body.ns-0 .mw-parser-output .sistersitebox{display:none!important}}@media screen{html.skin-theme-clientpref-night .mw-parser-output .sistersitebox img[src*="Wiktionary-logo-en-v2.svg"]{background-color:white}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .sistersitebox img[src*="Wiktionary-logo-en-v2.svg"]{background-color:white}}


/* end https://en.wikipedia.org/ */
</style><div class="side-box side-box-right sistersitebox"><style data-mw-deduplicate="TemplateStyles:r1126788409">
/* start https://en.wikipedia.org/ */


.mw-parser-output .plainlist ol,.mw-parser-output .plainlist ul{line-height:inherit;list-style:none;margin:0;padding:0}.mw-parser-output .plainlist ol li,.mw-parser-output .plainlist ul li{margin-bottom:0}


/* end https://en.wikipedia.org/ */
</style>
<div class="side-box-flex">
<div class="side-box-image"><span class="noviewer" typeof="mw:File"></span></div>
<div class="side-box-text plainlist">The Wikibook <i><a href="https://en.wikibooks.org/wiki/Ada_Programming" class="extiw external" title="wikibooks:Ada Programming">Ada Programming</a></i> has a page on the topic of: <i><b><a href="https://en.wikibooks.org/wiki/Ada_Programming/Control" class="extiw external" title="wikibooks:Ada Programming/Control">Control</a></b></i></div></div>
</div>
<div class="side-box side-box-right sistersitebox">
<div class="side-box-flex">
<div class="side-box-image"><span class="noviewer" typeof="mw:File"></span></div>
<div class="side-box-text plainlist">The Wikibook <i><a href="https://en.wikibooks.org/wiki/Computer_Programming" class="extiw external" title="wikibooks:Computer Programming">Computer Programming</a></i> has a page on the topic of: <i><b><a href="https://en.wikibooks.org/wiki/Computer_Programming/Control" class="extiw external" title="wikibooks:Computer Programming/Control">Control</a></b></i></div></div>
</div>
<ul><li><span class="noviewer" typeof="mw:File"></span> Media related to <a href="https://commons.wikimedia.org/wiki/Category:Control_flow" class="extiw external" title="commons:Category:Control flow">Control flow</a> at Wikimedia Commons</li>
<li><a rel="nofollow" class="external text" href="https://web.archive.org/web/20070703050443/http://www.acm.org/classics/oct95/">Go To Statement Considered Harmful</a></li>
<li><a rel="nofollow" class="external text" href="https://web.archive.org/web/20180716171336/http://www.fortran.com/fortran/come_from.html">A Linguistic Contribution of GOTO-less Programming</a></li>
<li><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20090824073244/http://pplab.snu.ac.kr/courses/adv_pl05/papers/p261-knuth.pdf">"Structured Programming with Go To Statements"</a> <span class="cs1-format">(PDF)</span>. Archived from <a rel="nofollow" class="external text" href="http://pplab.snu.ac.kr/courses/adv_pl05/papers/p261-knuth.pdf">the original</a> <span class="cs1-format">(PDF)</span> on 2009-08-24.</cite>&nbsp;<span style="font-size: 85%;">(2.88&nbsp;MB)</span></li>
<li><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.bitsavers.org/pdf/ibm/704/24-6661-2_704_Manual_1955.pdf">"IBM 704 Manual"</a> <span class="cs1-format">(PDF)</span>.</cite>&nbsp;<span style="font-size: 85%;">(31.4&nbsp;MB)</span></li></ul></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-07-31" href="https://en.wikipedia.org/wiki/?title=Control_flow&amp;oldid=1303454048">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>

</body></html>